src/java.base/share/classes/java/util/Arrays.java
author jboes
Wed, 27 Nov 2019 15:13:58 +0000
changeset 59298 de2896bc018a
parent 59055 57ad70bcf06c
permissions -rw-r--r--
8234799: javadoc of java.util.Arrays.compare(T[] a, T[] b) misses the word "less" Summary: Add word "less" to method-level javadoc Reviewed-by: dfuchs, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 51884
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4233
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4233
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4233
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4233
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4233
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
    28
import jdk.internal.HotSpotIntrinsicCandidate;
48356
29e165bdf669 8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents: 47216
diff changeset
    29
import jdk.internal.util.ArraysSupport;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
    30
51884
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
    31
import java.io.Serializable;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    32
import java.lang.reflect.Array;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    33
import java.util.concurrent.ForkJoinPool;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    34
import java.util.function.BinaryOperator;
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
    35
import java.util.function.Consumer;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    36
import java.util.function.DoubleBinaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    37
import java.util.function.IntBinaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    38
import java.util.function.IntFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    39
import java.util.function.IntToDoubleFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    40
import java.util.function.IntToLongFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    41
import java.util.function.IntUnaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    42
import java.util.function.LongBinaryOperator;
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
    43
import java.util.function.UnaryOperator;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    44
import java.util.stream.DoubleStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    45
import java.util.stream.IntStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    46
import java.util.stream.LongStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    47
import java.util.stream.Stream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    48
import java.util.stream.StreamSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * This class contains various methods for manipulating arrays (such as
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    52
 * sorting and searching). This class also contains a static factory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * that allows arrays to be viewed as lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    55
 * <p>The methods in this class all throw a {@code NullPointerException},
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    56
 * if the specified array reference is null, except where noted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>The documentation for the methods contained in this class includes
22954
617b9c3a2c65 8033943: Typo in the documentation for the class Arrays
igerasim
parents: 22285
diff changeset
    59
 * brief descriptions of the <i>implementations</i>. Such descriptions should
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * be regarded as <i>implementation notes</i>, rather than parts of the
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    61
 * <i>specification</i>. Implementors should feel free to substitute other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    62
 * algorithms, so long as the specification itself is adhered to. (For
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    63
 * example, the algorithm used by {@code sort(Object[])} does not have to be
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    64
 * a MergeSort, but it does have to be <i>stable</i>.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>This class is a member of the
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48356
diff changeset
    67
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    70
 * @author Josh Bloch
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    71
 * @author Neal Gafter
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    72
 * @author John Rose
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    73
 * @since  1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    75
public class Arrays {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // Suppresses default constructor, ensuring non-instantiability.
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    78
    private Arrays() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    80
    /*
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    81
     * Sorting methods. Note that all public "sort" methods take the
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    82
     * same form: performing argument checks if necessary, and then
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    83
     * expanding arguments into those required for the internal
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    84
     * implementation methods residing in other package-private
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    85
     * classes (except for legacyMergeSort, included in this class).
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    86
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    87
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    88
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    89
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    90
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    91
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    92
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    93
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    94
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    95
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    96
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    97
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    98
    public static void sort(int[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
    99
        DualPivotQuicksort.sort(a, 0, 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   100
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   101
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   102
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   103
     * Sorts the specified range of the array into ascending order. The range
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   104
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   105
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   106
     * the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   107
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   108
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   109
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   110
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   111
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   112
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   113
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   114
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   115
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   116
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   117
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   118
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   119
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   120
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   121
    public static void sort(int[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   122
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   123
        DualPivotQuicksort.sort(a, 0, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   124
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   125
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   126
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   127
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   128
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   129
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   130
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   131
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   132
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   133
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   134
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   135
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   136
    public static void sort(long[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   137
        DualPivotQuicksort.sort(a, 0, 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   138
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   139
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   140
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   141
     * Sorts the specified range of the array into ascending order. The range
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   142
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   143
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   144
     * the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   145
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   146
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   147
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   148
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   149
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   150
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   151
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   152
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   153
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   154
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   155
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   156
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   157
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   158
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   159
    public static void sort(long[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   160
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   161
        DualPivotQuicksort.sort(a, 0, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   162
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   163
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   164
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   165
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   166
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   167
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   168
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   169
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   170
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   171
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   172
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   173
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   174
    public static void sort(short[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   175
        DualPivotQuicksort.sort(a, 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   176
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   177
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   178
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   179
     * Sorts the specified range of the array into ascending order. The range
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   180
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   181
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   182
     * the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   183
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   184
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   185
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   186
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   187
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   188
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   189
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   190
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   191
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   192
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   193
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   194
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   195
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   196
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   197
    public static void sort(short[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   198
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   199
        DualPivotQuicksort.sort(a, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   200
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   201
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   202
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   203
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   204
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   205
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   206
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   207
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   208
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   209
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   210
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   211
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   212
    public static void sort(char[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   213
        DualPivotQuicksort.sort(a, 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   214
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   215
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   216
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   217
     * Sorts the specified range of the array into ascending order. The range
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   218
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   219
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   220
     * the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   221
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   222
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   223
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   224
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   225
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   226
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   227
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   228
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   229
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   230
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   231
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   232
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   233
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   234
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   235
    public static void sort(char[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   236
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   237
        DualPivotQuicksort.sort(a, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   238
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   239
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   240
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   241
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   242
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   243
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   244
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   245
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   246
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   247
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   248
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   249
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   250
    public static void sort(byte[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   251
        DualPivotQuicksort.sort(a, 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   252
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   253
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   254
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   255
     * Sorts the specified range of the array into ascending order. The range
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   256
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   257
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   258
     * the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   259
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   260
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   261
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   262
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   263
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   264
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   265
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   266
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   267
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   268
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   269
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   270
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   271
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   272
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   273
    public static void sort(byte[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   274
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   275
        DualPivotQuicksort.sort(a, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   276
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   277
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   278
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   279
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   280
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   281
     * <p>The {@code <} relation does not provide a total order on all float
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   282
     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   283
     * value compares neither less than, greater than, nor equal to any value,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   284
     * even itself. This method uses the total order imposed by the method
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   285
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   286
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   287
     * other value and all {@code Float.NaN} values are considered equal.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   288
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   289
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   290
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   291
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   292
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   293
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   294
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   295
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   296
    public static void sort(float[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   297
        DualPivotQuicksort.sort(a, 0, 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   298
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   299
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   300
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   301
     * Sorts the specified range of the array into ascending order. The range
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   302
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   303
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   304
     * the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   305
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   306
     * <p>The {@code <} relation does not provide a total order on all float
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   307
     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   308
     * value compares neither less than, greater than, nor equal to any value,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   309
     * even itself. This method uses the total order imposed by the method
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   310
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   311
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   312
     * other value and all {@code Float.NaN} values are considered equal.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   313
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   314
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   315
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   316
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   317
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   318
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   319
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   320
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   321
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   322
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   323
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   324
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   325
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   326
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   327
    public static void sort(float[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   328
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   329
        DualPivotQuicksort.sort(a, 0, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   330
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   331
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   332
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   333
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   334
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   335
     * <p>The {@code <} relation does not provide a total order on all double
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   336
     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   337
     * value compares neither less than, greater than, nor equal to any value,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   338
     * even itself. This method uses the total order imposed by the method
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   339
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   340
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   341
     * other value and all {@code Double.NaN} values are considered equal.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   342
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   343
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   344
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   345
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   346
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   347
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   348
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   349
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   350
    public static void sort(double[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   351
        DualPivotQuicksort.sort(a, 0, 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   352
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   353
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   354
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   355
     * Sorts the specified range of the array into ascending order. The range
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   356
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   357
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   358
     * the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   359
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   360
     * <p>The {@code <} relation does not provide a total order on all double
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   361
     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   362
     * value compares neither less than, greater than, nor equal to any value,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   363
     * even itself. This method uses the total order imposed by the method
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   364
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   365
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   366
     * other value and all {@code Double.NaN} values are considered equal.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   367
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   368
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   369
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   370
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   371
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   372
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   373
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   374
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   375
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   376
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   377
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   378
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   379
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   380
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   381
    public static void sort(double[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   382
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   383
        DualPivotQuicksort.sort(a, 0, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   384
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   385
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   386
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   387
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   388
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   389
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   390
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   391
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   392
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   393
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   394
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   395
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   396
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   397
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   398
    public static void parallelSort(byte[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   399
        DualPivotQuicksort.sort(a, 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   400
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   401
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   402
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   403
     * Sorts the specified range of the array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   404
     * The range to be sorted extends from the index {@code fromIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   405
     * inclusive, to the index {@code toIndex}, exclusive. If
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   406
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   407
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   408
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   409
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   410
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   411
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   412
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   413
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   414
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   415
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   416
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   417
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   418
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   419
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   420
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   421
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   422
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   423
    public static void parallelSort(byte[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   424
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   425
        DualPivotQuicksort.sort(a, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   426
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   427
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   428
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   429
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   430
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   431
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   432
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   433
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   434
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   435
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   436
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   437
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   438
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   439
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   440
    public static void parallelSort(char[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   441
        DualPivotQuicksort.sort(a, 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   442
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   443
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   444
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   445
     * Sorts the specified range of the array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   446
     * The range to be sorted extends from the index {@code fromIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   447
     * inclusive, to the index {@code toIndex}, exclusive. If
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   448
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   449
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   450
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   451
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   452
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   453
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   454
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   455
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   456
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   457
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   458
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   459
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   460
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   461
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   462
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   463
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   464
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   465
    public static void parallelSort(char[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   466
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   467
        DualPivotQuicksort.sort(a, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   468
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   469
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   470
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   471
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   472
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   473
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   474
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   475
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   476
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   477
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   478
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   479
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   480
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   481
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   482
    public static void parallelSort(short[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   483
        DualPivotQuicksort.sort(a, 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   484
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   485
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   486
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   487
     * Sorts the specified range of the array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   488
     * The range to be sorted extends from the index {@code fromIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   489
     * inclusive, to the index {@code toIndex}, exclusive. If
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   490
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   491
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   492
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   493
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   494
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   495
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   496
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   497
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   498
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   499
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   500
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   501
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   502
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   503
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   504
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   505
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   506
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   507
    public static void parallelSort(short[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   508
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   509
        DualPivotQuicksort.sort(a, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   510
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   511
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   512
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   513
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   514
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   515
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   516
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   517
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   518
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   519
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   520
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   521
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   522
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   523
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   524
    public static void parallelSort(int[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   525
        DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   526
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   527
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   528
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   529
     * Sorts the specified range of the array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   530
     * The range to be sorted extends from the index {@code fromIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   531
     * inclusive, to the index {@code toIndex}, exclusive. If
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   532
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   533
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   534
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   535
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   536
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   537
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   538
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   539
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   540
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   541
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   542
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   543
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   544
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   545
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   546
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   547
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   548
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   549
    public static void parallelSort(int[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   550
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   551
        DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   552
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   553
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   554
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   555
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   556
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   557
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   558
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   559
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   560
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   561
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   562
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   563
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   564
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   565
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   566
    public static void parallelSort(long[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   567
        DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   568
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   569
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   570
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   571
     * Sorts the specified range of the array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   572
     * The range to be sorted extends from the index {@code fromIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   573
     * inclusive, to the index {@code toIndex}, exclusive. If
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   574
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   575
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   576
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   577
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   578
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   579
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   580
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   581
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   582
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   583
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   584
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   585
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   586
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   587
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   588
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   589
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   590
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   591
    public static void parallelSort(long[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   592
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   593
        DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   594
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   595
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   596
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   597
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   598
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   599
     * <p>The {@code <} relation does not provide a total order on all float
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   600
     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   601
     * value compares neither less than, greater than, nor equal to any value,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   602
     * even itself. This method uses the total order imposed by the method
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   603
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   604
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   605
     * other value and all {@code Float.NaN} values are considered equal.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   606
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   607
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   608
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   609
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   610
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   611
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   612
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   613
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   614
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   615
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   616
    public static void parallelSort(float[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   617
        DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   618
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   619
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   620
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   621
     * Sorts the specified range of the array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   622
     * The range to be sorted extends from the index {@code fromIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   623
     * inclusive, to the index {@code toIndex}, exclusive. If
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   624
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   625
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   626
     * <p>The {@code <} relation does not provide a total order on all float
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   627
     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   628
     * value compares neither less than, greater than, nor equal to any value,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   629
     * even itself. This method uses the total order imposed by the method
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   630
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   631
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   632
     * other value and all {@code Float.NaN} values are considered equal.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   633
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   634
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   635
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   636
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   637
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   638
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   639
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   640
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   641
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   642
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   643
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   644
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   645
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   646
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   647
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   648
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   649
    public static void parallelSort(float[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   650
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   651
        DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   652
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   653
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   654
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   655
     * Sorts the specified array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   656
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   657
     * <p>The {@code <} relation does not provide a total order on all double
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   658
     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   659
     * value compares neither less than, greater than, nor equal to any value,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   660
     * even itself. This method uses the total order imposed by the method
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   661
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   662
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   663
     * other value and all {@code Double.NaN} values are considered equal.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   664
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   665
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   666
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   667
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   668
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   669
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   670
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   671
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   672
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   673
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   674
    public static void parallelSort(double[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   675
        DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), 0, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   676
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   677
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   678
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   679
     * Sorts the specified range of the array into ascending numerical order.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   680
     * The range to be sorted extends from the index {@code fromIndex},
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   681
     * inclusive, to the index {@code toIndex}, exclusive. If
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   682
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   683
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   684
     * <p>The {@code <} relation does not provide a total order on all double
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   685
     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   686
     * value compares neither less than, greater than, nor equal to any value,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   687
     * even itself. This method uses the total order imposed by the method
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   688
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   689
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   690
     * other value and all {@code Double.NaN} values are considered equal.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   691
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   692
     * @implNote The sorting algorithm is a Dual-Pivot Quicksort by
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   693
     * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   694
     * offers O(n log(n)) performance on all data sets, and is typically
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   695
     * faster than traditional (one-pivot) Quicksort implementations.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   696
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   697
     * @param a the array to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   698
     * @param fromIndex the index of the first element, inclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   699
     * @param toIndex the index of the last element, exclusive, to be sorted
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   700
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   701
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   702
     * @throws ArrayIndexOutOfBoundsException
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   703
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   704
     *
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   705
     * @since 1.8
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   706
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   707
    public static void parallelSort(double[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   708
        rangeCheck(a.length, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   709
        DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   710
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   711
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   712
    /**
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   713
     * Checks that {@code fromIndex} and {@code toIndex} are in
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   714
     * the range and throws an exception if they aren't.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   715
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   716
    static void rangeCheck(int arrayLength, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   717
        if (fromIndex > toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   718
            throw new IllegalArgumentException(
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   719
                "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   720
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   721
        if (fromIndex < 0) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   722
            throw new ArrayIndexOutOfBoundsException(fromIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   723
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   724
        if (toIndex > arrayLength) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   725
            throw new ArrayIndexOutOfBoundsException(toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   726
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   727
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   728
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   729
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   730
     * A comparator that implements the natural ordering of a group of
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   731
     * mutually comparable elements. May be used when a supplied
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   732
     * comparator is null. To simplify code-sharing within underlying
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   733
     * implementations, the compare method only declares type Object
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   734
     * for its second argument.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   735
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   736
     * Arrays class implementor's note: It is an empirical matter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   737
     * whether ComparableTimSort offers any performance benefit over
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   738
     * TimSort used with this comparator.  If not, you are better off
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   739
     * deleting or bypassing ComparableTimSort.  There is currently no
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   740
     * empirical case for separating them for parallel sorting, so all
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   741
     * public Object parallelSort methods use the same comparator
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   742
     * based implementation.
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
    static final class NaturalOrder implements Comparator<Object> {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   745
        @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   746
        public int compare(Object first, Object second) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   747
            return ((Comparable<Object>)first).compareTo(second);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   748
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   749
        static final NaturalOrder INSTANCE = new NaturalOrder();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   750
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   751
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   752
    /**
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   753
     * The minimum array length below which a parallel sorting
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   754
     * algorithm will not further partition the sorting task. Using
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   755
     * smaller sizes typically results in memory contention across
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   756
     * tasks that makes parallel speedups unlikely.
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   757
     */
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 58520
diff changeset
   758
    private static final int MIN_ARRAY_SORT_GRAN = 1 << 13;
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   759
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   760
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   761
     * Sorts the specified array of objects into ascending order, according
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   762
     * to the {@linkplain Comparable natural ordering} of its elements.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   763
     * All elements in the array must implement the {@link Comparable}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   764
     * interface.  Furthermore, all elements in the array must be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   765
     * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   766
     * not throw a {@code ClassCastException} for any elements {@code e1}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   767
     * and {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   768
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   769
     * <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
   770
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   771
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   772
     * @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
   773
     * 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
   774
     * 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
   775
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   776
     * 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
   777
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   778
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   779
     * 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
   780
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   781
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   782
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   783
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   784
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   785
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   786
     * @throws ClassCastException if the array contains elements that are not
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   787
     *         <i>mutually comparable</i> (for example, strings and integers)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   788
     * @throws IllegalArgumentException (optional) if the natural
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   789
     *         ordering of the array elements is found to violate the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   790
     *         {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   791
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   792
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   793
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   794
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   795
    public static <T extends Comparable<? super T>> void parallelSort(T[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   796
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   797
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   798
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   799
            TimSort.sort(a, 0, n, NaturalOrder.INSTANCE, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   800
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
   801
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   802
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   803
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   804
                 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   805
                 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
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
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   808
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   809
     * Sorts the specified range of the specified array of objects into
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   810
     * ascending order, according to the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   811
     * {@linkplain Comparable natural ordering} of its
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   812
     * elements.  The range to be sorted extends from index
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   813
     * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   814
     * (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
   815
     * elements in this range must implement the {@link Comparable}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   816
     * interface.  Furthermore, all elements in this range must be <i>mutually
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   817
     * 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
   818
     * {@code ClassCastException} for any elements {@code e1} and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   819
     * {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   820
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   821
     * <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
   822
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   823
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   824
     * @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
   825
     * 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
   826
     * 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
   827
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   828
     * 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
   829
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   830
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   831
     * 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
   832
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   833
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   834
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   835
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   836
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   837
     * @param fromIndex the index of the first element (inclusive) to be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   838
     *        sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   839
     * @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
   840
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   841
     *         (optional) if the natural ordering of the array elements is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   842
     *         found to violate the {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   843
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   844
     *         {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   845
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   846
     *         not <i>mutually comparable</i> (for example, strings and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   847
     *         integers).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   848
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   849
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   850
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   851
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   852
    public static <T extends Comparable<? super T>>
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   853
    void parallelSort(T[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   854
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   855
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   856
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   857
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   858
            TimSort.sort(a, fromIndex, toIndex, NaturalOrder.INSTANCE, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   859
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
   860
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   861
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   862
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   863
                 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   864
                 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   865
    }
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
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   868
     * 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
   869
     * the specified comparator.  All elements in the array must be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   870
     * <i>mutually comparable</i> by the specified comparator (that is,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   871
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   872
     * for any elements {@code e1} and {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   873
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   874
     * <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
   875
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   876
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   877
     * @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
   878
     * 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
   879
     * 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
   880
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   881
     * 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
   882
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   883
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   884
     * 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
   885
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   886
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   887
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   888
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   889
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   890
     * @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
   891
     *        {@code null} value indicates that the elements'
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   892
     *        {@linkplain Comparable natural ordering} should be used.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   893
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   894
     *         not <i>mutually comparable</i> using the specified comparator
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   895
     * @throws IllegalArgumentException (optional) if the comparator is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   896
     *         found to violate the {@link java.util.Comparator} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   897
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   898
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   899
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   900
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   901
    public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   902
        if (cmp == null)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   903
            cmp = NaturalOrder.INSTANCE;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   904
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   905
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   906
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   907
            TimSort.sort(a, 0, n, cmp, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   908
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
   909
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   910
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   911
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   912
                 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   913
                 MIN_ARRAY_SORT_GRAN : g, cmp).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   914
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   915
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   916
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   917
     * Sorts the specified range of the specified array of objects according
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   918
     * 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
   919
     * sorted extends from index {@code fromIndex}, inclusive, to index
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   920
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   921
     * 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
   922
     * <i>mutually comparable</i> by the specified comparator (that is,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   923
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   924
     * for any elements {@code e1} and {@code e2} in the range).
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>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
   927
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   928
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   929
     * @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
   930
     * 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
   931
     * 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
   932
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   933
     * 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
   934
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   935
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   936
     * 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
   937
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   938
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   939
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   940
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   941
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   942
     * @param fromIndex the index of the first element (inclusive) to be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   943
     *        sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   944
     * @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
   945
     * @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
   946
     *        {@code null} value indicates that the elements'
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   947
     *        {@linkplain Comparable natural ordering} should be used.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   948
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   949
     *         (optional) if the natural ordering of the array elements is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   950
     *         found to violate the {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   951
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   952
     *         {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   953
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   954
     *         not <i>mutually comparable</i> (for example, strings and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   955
     *         integers).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   956
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   957
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   958
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   959
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   960
    public static <T> void parallelSort(T[] a, int fromIndex, int toIndex,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   961
                                        Comparator<? super T> cmp) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   962
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   963
        if (cmp == null)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   964
            cmp = NaturalOrder.INSTANCE;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   965
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   966
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   967
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   968
            TimSort.sort(a, fromIndex, toIndex, cmp, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   969
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
   970
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   971
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   972
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   973
                 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   974
                 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
   975
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   977
    /*
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   978
     * Sorting of complex type arrays.
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   979
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   981
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   982
     * 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
   983
     * 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
   984
     * 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
   985
     * 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
   986
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   987
    static final class LegacyMergeSort {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   988
        private static final boolean userRequested =
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   989
            java.security.AccessController.doPrivileged(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   990
                new sun.security.action.GetBooleanAction(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   991
                    "java.util.Arrays.useLegacyMergeSort")).booleanValue();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   992
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   993
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    /**
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   995
     * 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
   996
     * 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
   997
     * 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
   998
     * 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
   999
     * <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
  1000
     * 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
  1001
     * and {@code e2} in the array).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1002
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1003
     * <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
  1004
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1006
     * <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
  1007
     * 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
  1008
     * 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
  1009
     * 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
  1010
     * 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
  1011
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1012
     * 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
  1013
     * 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
  1014
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1016
     * <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
  1017
     * 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
  1018
     * 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
  1019
     * 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
  1020
     * 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
  1021
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1022
     * <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
  1023
     * (<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
  1024
     * 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
  1025
     * 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
  1026
     * 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
  1027
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @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
  1030
     * @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
  1031
     *         <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
  1032
     * @throws IllegalArgumentException (optional) if the natural
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1033
     *         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
  1034
     *         {@link Comparable} contract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    public static void sort(Object[] a) {
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1037
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1038
            legacyMergeSort(a);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1039
        else
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1040
            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
  1041
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1042
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1043
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1044
    private static void legacyMergeSort(Object[] a) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1045
        Object[] aux = a.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        mergeSort(aux, a, 0, a.length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * Sorts the specified range of the specified array of objects into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * ascending order, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * {@linkplain Comparable natural ordering} of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * 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
  1054
     * {@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
  1055
     * (If {@code fromIndex==toIndex}, the range to be sorted is empty.)  All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * elements in this range must implement the {@link Comparable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * 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
  1058
     * 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
  1059
     * {@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
  1060
     * {@code e2} in the array).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1061
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1062
     * <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
  1063
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1065
     * <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
  1066
     * 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
  1067
     * 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
  1068
     * 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
  1069
     * 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
  1070
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1071
     * 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
  1072
     * 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
  1073
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1075
     * <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
  1076
     * 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
  1077
     * 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
  1078
     * 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
  1079
     * 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
  1080
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1081
     * <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
  1082
     * (<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
  1083
     * 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
  1084
     * 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
  1085
     * 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
  1086
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     *        sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * @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
  1092
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1093
     *         (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
  1094
     *         found to violate the {@link Comparable} contract
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1095
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1096
     *         {@code toIndex > a.length}
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1097
     * @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
  1098
     *         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
  1099
     *         integers).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    public static void sort(Object[] a, int fromIndex, int toIndex) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1102
        rangeCheck(a.length, fromIndex, toIndex);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1103
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1104
            legacyMergeSort(a, fromIndex, toIndex);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1105
        else
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1106
            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
  1107
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1108
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1109
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1110
    private static void legacyMergeSort(Object[] a,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1111
                                        int fromIndex, int toIndex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        Object[] aux = copyOfRange(a, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * 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
  1118
     * used in preference to mergesort.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1119
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    private static final int INSERTIONSORT_THRESHOLD = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * Src is the source array that starts at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * Dest is the (possibly larger) array destination with a possible offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * low is the index in dest to start sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * high is the end index in dest to end sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * 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
  1129
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  1131
    @SuppressWarnings({"unchecked", "rawtypes"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    private static void mergeSort(Object[] src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                                  Object[] dest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                                  int low,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                                  int high,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                                  int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        int length = high - low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        // Insertion sort on smallest arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        if (length < INSERTIONSORT_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            for (int i=low; i<high; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                for (int j=i; j>low &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                         ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                    swap(dest, j, j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        // Recursively sort halves of dest into src
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        int destLow  = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        int destHigh = high;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        low  += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        high += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        mergeSort(dest, src, low, mid, -off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        mergeSort(dest, src, mid, high, -off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        // If list is already sorted, just copy from src to dest.  This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        // optimization that results in faster sorts for nearly ordered lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            System.arraycopy(src, low, dest, destLow, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        // Merge sorted halves (now in src) into dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                dest[i] = src[p++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                dest[i] = src[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * Swaps x[a] with x[b].
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    private static void swap(Object[] x, int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        Object t = x[a];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        x[a] = x[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        x[b] = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * Sorts the specified array of objects according to the order induced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * the specified comparator.  All elements in the array must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * <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
  1186
     * {@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
  1187
     * 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
  1188
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1189
     * <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
  1190
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1192
     * <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
  1193
     * 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
  1194
     * 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
  1195
     * 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
  1196
     * 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
  1197
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1198
     * 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
  1199
     * 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
  1200
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1202
     * <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
  1203
     * 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
  1204
     * 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
  1205
     * 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
  1206
     * 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
  1207
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1208
     * <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
  1209
     * (<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
  1210
     * 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
  1211
     * 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
  1212
     * 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
  1213
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1215
     * @param <T> the class of the objects to be sorted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * @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
  1218
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *        {@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
  1220
     * @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
  1221
     *         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
  1222
     * @throws IllegalArgumentException (optional) if the comparator is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1223
     *         found to violate the {@link Comparator} contract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    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
  1226
        if (c == null) {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1227
            sort(a);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1228
        } else {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1229
            if (LegacyMergeSort.userRequested)
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1230
                legacyMergeSort(a, c);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1231
            else
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1232
                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
  1233
        }
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1234
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1235
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1236
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1237
    private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1238
        T[] aux = a.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            mergeSort(aux, a, 0, a.length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            mergeSort(aux, a, 0, a.length, 0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * Sorts the specified range of the specified array of objects according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * 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
  1248
     * 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
  1249
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * range to be sorted is empty.)  All elements in the range must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * <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
  1252
     * {@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
  1253
     * 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
  1254
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1255
     * <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
  1256
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1258
     * <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
  1259
     * 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
  1260
     * 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
  1261
     * 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
  1262
     * 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
  1263
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1264
     * 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
  1265
     * 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
  1266
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1268
     * <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
  1269
     * 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
  1270
     * 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
  1271
     * 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
  1272
     * 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
  1273
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1274
     * <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
  1275
     * (<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
  1276
     * 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
  1277
     * 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
  1278
     * 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
  1279
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1281
     * @param <T> the class of the objects to be sorted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     *        sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * @param toIndex the index of the last element (exclusive) to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * @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
  1287
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * @throws ClassCastException if the array contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     *         <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
  1291
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1292
     *         (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
  1293
     *         {@link Comparator} contract
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1294
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1295
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    public static <T> void sort(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                                Comparator<? super T> c) {
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1299
        if (c == null) {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1300
            sort(a, fromIndex, toIndex);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1301
        } else {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1302
            rangeCheck(a.length, fromIndex, toIndex);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1303
            if (LegacyMergeSort.userRequested)
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1304
                legacyMergeSort(a, fromIndex, toIndex, c);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1305
            else
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1306
                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
  1307
        }
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1308
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1309
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1310
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1311
    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
  1312
                                            Comparator<? super T> c) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1313
        T[] aux = copyOfRange(a, fromIndex, toIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * Src is the source array that starts at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * Dest is the (possibly larger) array destination with a possible offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * low is the index in dest to start sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * high is the end index in dest to end sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * 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
  1326
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  1328
    @SuppressWarnings({"rawtypes", "unchecked"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    private static void mergeSort(Object[] src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                                  Object[] dest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                                  int low, int high, int off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                                  Comparator c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        int length = high - low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        // Insertion sort on smallest arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        if (length < INSERTIONSORT_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            for (int i=low; i<high; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                    swap(dest, j, j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        // Recursively sort halves of dest into src
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        int destLow  = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        int destHigh = high;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        low  += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        high += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        mergeSort(dest, src, low, mid, -off, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        mergeSort(dest, src, mid, high, -off, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        // If list is already sorted, just copy from src to dest.  This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        // optimization that results in faster sorts for nearly ordered lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        if (c.compare(src[mid-1], src[mid]) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
           System.arraycopy(src, low, dest, destLow, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        // Merge sorted halves (now in src) into dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                dest[i] = src[p++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                dest[i] = src[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1368
    // Parallel prefix
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1369
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1370
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1371
     * 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
  1372
     * 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
  1373
     * 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
  1374
     * 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
  1375
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1376
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1377
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1378
     * @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
  1379
     * @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
  1380
     * @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
  1381
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1382
     * @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
  1383
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1384
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1385
    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
  1386
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1387
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1388
            new ArrayPrefixHelpers.CumulateTask<>
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1389
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1390
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1391
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1392
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1393
     * Performs {@link #parallelPrefix(Object[], BinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1394
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1395
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1396
     * @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
  1397
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1398
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1399
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1400
     * @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
  1401
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1402
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1403
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1404
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1405
     * @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
  1406
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1407
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1408
    public static <T> void parallelPrefix(T[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1409
                                          int toIndex, BinaryOperator<T> op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1410
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1411
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1412
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1413
            new ArrayPrefixHelpers.CumulateTask<>
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1414
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1415
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1416
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1417
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1418
     * 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
  1419
     * 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
  1420
     * 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
  1421
     * 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
  1422
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1423
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1424
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1425
     * @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
  1426
     * @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
  1427
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1428
     * @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
  1429
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1430
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1431
    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
  1432
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1433
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1434
            new ArrayPrefixHelpers.LongCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1435
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1436
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1437
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1438
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1439
     * Performs {@link #parallelPrefix(long[], LongBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1440
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1441
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1442
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1443
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1444
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1445
     * @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
  1446
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1447
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1448
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1449
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1450
     * @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
  1451
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1452
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1453
    public static void parallelPrefix(long[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1454
                                      int toIndex, LongBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1455
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1456
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1457
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1458
            new ArrayPrefixHelpers.LongCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1459
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1460
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1461
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1462
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1463
     * 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
  1464
     * 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
  1465
     * 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
  1466
     * 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
  1467
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1468
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1469
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1470
     * <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
  1471
     * 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
  1472
     * obtained if the operation was performed sequentially.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1473
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1474
     * @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
  1475
     * @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
  1476
     * @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
  1477
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1478
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1479
    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
  1480
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1481
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1482
            new ArrayPrefixHelpers.DoubleCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1483
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1484
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1485
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1486
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1487
     * Performs {@link #parallelPrefix(double[], DoubleBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1488
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1489
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1490
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1491
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1492
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1493
     * @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
  1494
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1495
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1496
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1497
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1498
     * @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
  1499
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1500
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1501
    public static void parallelPrefix(double[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1502
                                      int toIndex, DoubleBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1503
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1504
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1505
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1506
            new ArrayPrefixHelpers.DoubleCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1507
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1508
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1509
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1510
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1511
     * 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
  1512
     * 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
  1513
     * 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
  1514
     * 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
  1515
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1516
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1517
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1518
     * @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
  1519
     * @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
  1520
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1521
     * @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
  1522
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1523
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1524
    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
  1525
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1526
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1527
            new ArrayPrefixHelpers.IntCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1528
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1529
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1530
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1531
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1532
     * Performs {@link #parallelPrefix(int[], IntBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1533
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1534
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1535
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1536
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1537
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1538
     * @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
  1539
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1540
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1541
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1542
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1543
     * @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
  1544
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1545
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1546
    public static void parallelPrefix(int[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1547
                                      int toIndex, IntBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1548
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1549
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1550
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1551
            new ArrayPrefixHelpers.IntCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1552
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1553
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1554
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
    // Searching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * Searches the specified array of longs for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * by the {@link #sort(long[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * @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
  1568
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     *         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
  1571
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    public static int binarySearch(long[] a, long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     * the specified array of longs for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * by the {@link #sort(long[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1598
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     *         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
  1602
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    public static int binarySearch(long[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                                   long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    private static int binarySearch0(long[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                                     long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            long midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * Searches the specified array of ints for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * by the {@link #sort(int[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     * @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
  1649
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     *         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
  1652
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    public static int binarySearch(int[] a, int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * the specified array of ints for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     * by the {@link #sort(int[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1679
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
     *         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
  1683
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    public static int binarySearch(int[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                                   int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    private static int binarySearch0(int[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                                     int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            int midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * Searches the specified array of shorts for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     * the binary search algorithm.  The array must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * (as by the {@link #sort(short[])} method) prior to making this call.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * it is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * @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
  1730
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     *         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
  1733
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
    public static int binarySearch(short[] a, short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * the specified array of shorts for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * (as by the {@link #sort(short[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     * prior to making this call.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     * it is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1760
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     *         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
  1764
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    public static int binarySearch(short[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                                   short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
    private static int binarySearch0(short[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                                     short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
            short midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     * Searches the specified array of chars for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     * by the {@link #sort(char[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     * @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
  1811
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     *         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
  1814
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    public static int binarySearch(char[] a, char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     * the specified array of chars for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     * by the {@link #sort(char[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1841
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     *         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
  1845
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
    public static int binarySearch(char[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                                   char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
    private static int binarySearch0(char[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                                     char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            char midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * Searches the specified array of bytes for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * by the {@link #sort(byte[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * @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
  1892
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     *         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
  1895
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
    public static int binarySearch(byte[] a, byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * the specified array of bytes for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * by the {@link #sort(byte[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1922
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     *         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
  1926
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    public static int binarySearch(byte[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                                   byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
    private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                                     byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            byte midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     * Searches the specified array of doubles for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     * the binary search algorithm.  The array must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     * (as by the {@link #sort(double[])} method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * If it is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     * one will be found.  This method considers all NaN values to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     * @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
  1974
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     *         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
  1977
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
    public static int binarySearch(double[] a, double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     * the specified array of doubles for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     * (as by the {@link #sort(double[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
     * prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
     * If it is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
     * one will be found.  This method considers all NaN values to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2005
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     *         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
  2009
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
    public static int binarySearch(double[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                                   double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
    private static int binarySearch0(double[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                                     double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            double midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                high = mid - 1; // Neither val is NaN, thisVal is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                long midBits = Double.doubleToLongBits(midVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                long keyBits = Double.doubleToLongBits(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                if (midBits == keyBits)     // Values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                    return mid;             // Key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                    low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
                else                        // (0.0, -0.0) or (NaN, !NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                    high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * 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
  2055
     * 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
  2056
     * (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
  2057
     * it is not sorted, the results are undefined. If the array contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * 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
  2059
     * one will be found. This method considers all NaN values to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * @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
  2065
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     *         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
  2068
     *         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
  2069
     *         elements in the array are less than the specified key. Note
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
    public static int binarySearch(float[] a, float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     * the specified array of floats for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     * (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
  2083
     * prior to making this call. If
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  2084
     * it is not sorted, the results are undefined. If the range contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
     * 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
  2086
     * one will be found. This method considers all NaN values to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2096
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     *         element 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
  2100
     *         or {@code toIndex} if all
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  2101
     *         elements in the range are less than the specified key. Note
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    public static int binarySearch(float[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                                   float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
    private static int binarySearch0(float[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                                     float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
            float midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                high = mid - 1; // Neither val is NaN, thisVal is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                int midBits = Float.floatToIntBits(midVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                int keyBits = Float.floatToIntBits(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                if (midBits == keyBits)     // Values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                    return mid;             // Key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                    low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                else                        // (0.0, -0.0) or (NaN, !NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                    high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     * 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
  2146
     * search algorithm. The array must be sorted into ascending order
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     * according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     * {@linkplain Comparable natural ordering}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * of its elements (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     * {@link #sort(Object[])} method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     * If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     * (If the array contains elements that are not mutually comparable (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     * example, strings and integers), it <i>cannot</i> be sorted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
     * to the natural ordering of its elements, hence results are undefined.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
     * If the array contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * elements equal to the specified object, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     * @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
  2162
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
     *         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
  2165
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     * @throws ClassCastException if the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     *         elements of the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
    public static int binarySearch(Object[] a, Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     * the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     * search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
     * The range must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
     * according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
     * {@linkplain Comparable natural ordering}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     * of its elements (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     * {@link #sort(Object[], int, int)} method) prior to making this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     * call.  If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     * (If the range contains elements that are not mutually comparable (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     * example, strings and integers), it <i>cannot</i> be sorted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     * to the natural ordering of its elements, hence results are undefined.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     * If the range contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     * elements equal to the specified object, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2200
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     *         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
  2204
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     * @throws ClassCastException if the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     *         elements of the array within the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
    public static int binarySearch(Object[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                                   Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
    private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                                     Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
            int mid = (low + high) >>> 1;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2230
            @SuppressWarnings("rawtypes")
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  2231
            Comparable midVal = (Comparable)a[mid];
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2232
            @SuppressWarnings("unchecked")
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  2233
            int cmp = midVal.compareTo(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     * Searches the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     * search algorithm.  The array must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     * {@link #sort(Object[], Comparator) sort(T[], Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     * method) prior to making this call.  If it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
     * not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
     * If the array contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
     * elements equal to the specified object, there is no guarantee which one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
     * will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2256
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     * @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
  2260
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     * @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
  2263
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
     *         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
  2266
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     * @throws ClassCastException if the array contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
     *         or the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
     *         elements of the array using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
    public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
        return binarySearch0(a, 0, a.length, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     * the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     * search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
     * The range must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
     * {@link #sort(Object[], int, int, Comparator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
     * sort(T[], int, int, Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
     * method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
     * If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
     * If the range contains multiple elements equal to the specified object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     * there is no guarantee which one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2292
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     * @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
  2299
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2303
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
     *         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
  2307
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     * @throws ClassCastException if the range contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     *         or the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     *         elements in the range using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
    public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                                       T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
        return binarySearch0(a, fromIndex, toIndex, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
    private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                                         T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            T midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
            int cmp = c.compare(midVal, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
    // Equality Testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2353
     * Returns {@code true} if the two specified arrays of longs are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     * 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
  2358
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * @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
  2362
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
    public static boolean equals(long[] a, long[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2374
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2378
     * Returns true if the two specified arrays of longs, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2379
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2380
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2381
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2382
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2383
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2384
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2385
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2386
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2387
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2388
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2389
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2390
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2391
     *                 first array to be tested
59055
57ad70bcf06c 8233731: repeated typo "fro" for "for"
coffeys
parents: 59042
diff changeset
  2392
     * @param b the second array to be tested for equality
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2393
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2394
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2395
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2396
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2397
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2398
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2399
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2400
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2401
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2402
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2403
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2404
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2405
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2406
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2407
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2408
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2409
    public static boolean equals(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2410
                                 long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2411
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2412
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2413
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2414
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2415
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2416
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2417
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2418
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2419
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2420
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2421
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2422
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2423
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2424
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2425
     * Returns {@code true} if the two specified arrays of ints are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     * 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
  2430
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     * @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
  2434
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
    public static boolean equals(int[] a, int[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2446
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2450
     * Returns true if the two specified arrays of ints, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2451
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2452
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2453
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2454
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2455
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2456
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2457
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2458
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2459
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2460
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2461
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2462
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2463
     *                 first array to be tested
59055
57ad70bcf06c 8233731: repeated typo "fro" for "for"
coffeys
parents: 59042
diff changeset
  2464
     * @param b the second array to be tested for equality
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2465
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2466
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2467
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2468
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2469
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2470
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2471
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2472
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2473
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2474
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2475
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2476
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2477
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2478
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2479
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2480
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2481
    public static boolean equals(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2482
                                 int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2483
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2484
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2485
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2486
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2487
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2488
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2489
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2490
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2491
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2492
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2493
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2494
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2495
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2496
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2497
     * Returns {@code true} if the two specified arrays of shorts are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
     * 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
  2502
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
     * @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
  2506
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
    public static boolean equals(short[] a, short a2[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2518
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2522
     * Returns true if the two specified arrays of shorts, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2523
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2524
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2525
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2526
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2527
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2528
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2529
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2530
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2531
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2532
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2533
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2534
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2535
     *                 first array to be tested
59055
57ad70bcf06c 8233731: repeated typo "fro" for "for"
coffeys
parents: 59042
diff changeset
  2536
     * @param b the second array to be tested for equality
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2537
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2538
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2539
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2540
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2541
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2542
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2543
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2544
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2545
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2546
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2547
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2548
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2549
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2550
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2551
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2552
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2553
    public static boolean equals(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2554
                                 short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2555
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2556
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2557
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2558
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2559
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2560
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2561
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2562
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2563
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2564
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2565
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2566
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2567
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2568
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2569
     * Returns {@code true} if the two specified arrays of chars are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
     * 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
  2574
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
     * @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
  2578
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29223
diff changeset
  2580
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
    public static boolean equals(char[] a, char[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2591
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2595
     * Returns true if the two specified arrays of chars, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2596
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2597
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2598
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2599
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2600
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2601
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2602
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2603
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2604
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2605
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2606
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2607
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2608
     *                 first array to be tested
59055
57ad70bcf06c 8233731: repeated typo "fro" for "for"
coffeys
parents: 59042
diff changeset
  2609
     * @param b the second array to be tested for equality
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2610
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2611
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2612
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2613
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2614
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2615
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2616
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2617
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2618
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2619
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2620
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2621
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2622
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2623
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2624
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2625
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2626
    public static boolean equals(char[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2627
                                 char[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2628
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2629
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2630
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2631
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2632
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2633
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2634
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2635
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2636
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2637
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2638
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2639
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2640
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2641
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2642
     * Returns {@code true} if the two specified arrays of bytes are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
     * 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
  2647
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
     * @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
  2651
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
     */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32108
diff changeset
  2653
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
    public static boolean equals(byte[] a, byte[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2664
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2668
     * Returns true if the two specified arrays of bytes, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2669
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2670
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2671
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2672
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2673
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2674
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2675
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2676
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2677
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2678
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2679
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2680
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2681
     *                 first array to be tested
59055
57ad70bcf06c 8233731: repeated typo "fro" for "for"
coffeys
parents: 59042
diff changeset
  2682
     * @param b the second array to be tested for equality
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2683
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2684
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2685
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2686
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2687
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2688
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2689
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2690
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2691
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2692
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2693
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2694
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2695
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2696
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2697
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2698
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2699
    public static boolean equals(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2700
                                 byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2701
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2702
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2703
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2704
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2705
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2706
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2707
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2708
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2709
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2710
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2711
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2712
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2713
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2714
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2715
     * Returns {@code true} if the two specified arrays of booleans are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
     * 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
  2720
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
     * @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
  2724
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
    public static boolean equals(boolean[] a, boolean[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2736
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2740
     * Returns true if the two specified arrays of booleans, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2741
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2742
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2743
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2744
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2745
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2746
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2747
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2748
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2749
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2750
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2751
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2752
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2753
     *                 first array to be tested
59055
57ad70bcf06c 8233731: repeated typo "fro" for "for"
coffeys
parents: 59042
diff changeset
  2754
     * @param b the second array to be tested for equality
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2755
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2756
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2757
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2758
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2759
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2760
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2761
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2762
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2763
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2764
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2765
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2766
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2767
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2768
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2769
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2770
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2771
    public static boolean equals(boolean[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2772
                                 boolean[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2773
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2774
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2775
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2776
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2777
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2778
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2779
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2780
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2781
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2782
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2783
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2784
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2785
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2786
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2787
     * Returns {@code true} if the two specified arrays of doubles are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
     * 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
  2792
     * 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
  2793
     *
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2794
     * 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
  2795
     * <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
  2796
     * (Unlike the {@code ==} operator, this method considers
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2797
     * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
     * @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
  2801
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
     * @see Double#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
    public static boolean equals(double[] a, double[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2814
        return ArraysSupport.mismatch(a, a2, length) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2815
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2816
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2817
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2818
     * Returns true if the two specified arrays of doubles, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2819
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2820
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2821
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2822
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2823
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2824
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2825
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2826
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2827
     * <p>Two doubles {@code d1} and {@code d2} are considered equal if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2828
     * <pre>    {@code new Double(d1).equals(new Double(d2))}</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2829
     * (Unlike the {@code ==} operator, this method considers
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2830
     * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2831
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2832
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2833
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2834
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2835
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2836
     *                 first array to be tested
59055
57ad70bcf06c 8233731: repeated typo "fro" for "for"
coffeys
parents: 59042
diff changeset
  2837
     * @param b the second array to be tested for equality
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2838
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2839
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2840
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2841
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2842
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2843
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2844
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2845
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2846
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2847
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2848
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2849
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2850
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2851
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2852
     * @see Double#equals(Object)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2853
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2854
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2855
    public static boolean equals(double[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2856
                                 double[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2857
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2858
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2859
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2860
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2861
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2862
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2863
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2864
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2865
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2866
                                      b, bFromIndex, aLength) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2870
     * Returns {@code true} if the two specified arrays of floats are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
     * 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
  2875
     * 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
  2876
     *
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2877
     * 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
  2878
     * <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
  2879
     * (Unlike the {@code ==} operator, this method considers
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2880
     * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
     * @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
  2884
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
     * @see Float#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
    public static boolean equals(float[] a, float[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2897
        return ArraysSupport.mismatch(a, a2, length) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2898
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2899
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2900
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2901
     * Returns true if the two specified arrays of floats, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2902
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2903
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2904
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2905
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2906
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2907
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2908
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2909
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2910
     * <p>Two floats {@code f1} and {@code f2} are considered equal if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2911
     * <pre>    {@code new Float(f1).equals(new Float(f2))}</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2912
     * (Unlike the {@code ==} operator, this method considers
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2913
     * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2914
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2915
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2916
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2917
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2918
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2919
     *                 first array to be tested
59055
57ad70bcf06c 8233731: repeated typo "fro" for "for"
coffeys
parents: 59042
diff changeset
  2920
     * @param b the second array to be tested for equality
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2921
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2922
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2923
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2924
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2925
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2926
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2927
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2928
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2929
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2930
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2931
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2932
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2933
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2934
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2935
     * @see Float#equals(Object)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2936
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2937
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2938
    public static boolean equals(float[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2939
                                 float[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2940
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2941
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2942
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2943
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2944
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2945
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2946
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2947
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2948
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2949
                                      b, bFromIndex, aLength) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2953
     * Returns {@code true} if the two specified arrays of Objects are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
     * <i>equal</i> to one another.  The two arrays are considered equal if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
     * 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
  2956
     * 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
  2957
     * 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
  2958
     * {@code Objects.equals(e1, e2)}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2959
     * In other words, the two arrays are equal if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
     * 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
  2961
     * references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
     * @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
  2965
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
    public static boolean equals(Object[] a, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
        for (int i=0; i<length; i++) {
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2978
            if (!Objects.equals(a[i], a2[i]))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2979
                return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2980
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2981
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2982
        return true;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2983
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2984
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2985
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2986
     * Returns true if the two specified arrays of Objects, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2987
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2988
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2989
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2990
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2991
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2992
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2993
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2994
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2995
     * <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
  2996
     * {@code Objects.equals(e1, e2)}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2997
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2998
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2999
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3000
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3001
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3002
     *                 first array to be tested
59055
57ad70bcf06c 8233731: repeated typo "fro" for "for"
coffeys
parents: 59042
diff changeset
  3003
     * @param b the second array to be tested for equality
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3004
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3005
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3006
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3007
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3008
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3009
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3010
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3011
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3012
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3013
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3014
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3015
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3016
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3017
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3018
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3019
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3020
    public static boolean equals(Object[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3021
                                 Object[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3022
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3023
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3024
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3025
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3026
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3027
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3028
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3029
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3030
        for (int i = 0; i < aLength; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3031
            if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3038
    /**
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3039
     * Returns {@code true} if the two specified arrays of Objects are
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3040
     * <i>equal</i> to one another.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3041
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3042
     * <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
  3043
     * of elements, and all corresponding pairs of elements in the two arrays
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3044
     * 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
  3045
     * same elements in the same order.  Also, two array references are
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3046
     * considered equal if both are {@code null}.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3047
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3048
     * <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
  3049
     * given the specified comparator, {@code cmp.compare(e1, e2) == 0}.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3050
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3051
     * @param a one array to be tested for equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3052
     * @param a2 the other array to be tested for equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3053
     * @param cmp the comparator to compare array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3054
     * @param <T> the type of array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3055
     * @return {@code true} if the two arrays are equal
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3056
     * @throws NullPointerException if the comparator is {@code null}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3057
     * @since 9
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3058
     */
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3059
    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
  3060
        Objects.requireNonNull(cmp);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3061
        if (a==a2)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3062
            return true;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3063
        if (a==null || a2==null)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3064
            return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3065
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3066
        int length = a.length;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3067
        if (a2.length != length)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3068
            return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3069
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3070
        for (int i=0; i<length; i++) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3071
            if (cmp.compare(a[i], a2[i]) != 0)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3072
                return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3073
        }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3074
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3075
        return true;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3076
    }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3077
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3078
    /**
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3079
     * Returns true if the two specified arrays of Objects, over the specified
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3080
     * ranges, are <i>equal</i> to one another.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3081
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3082
     * <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
  3083
     * 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
  3084
     * 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
  3085
     * are equal if they contain, over the specified ranges, the same elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3086
     * in the same order.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3087
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3088
     * <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
  3089
     * given the specified comparator, {@code cmp.compare(e1, e2) == 0}.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3090
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3091
     * @param a the first array to be tested for equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3092
     * @param aFromIndex the index (inclusive) of the first element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3093
     *                   first array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3094
     * @param aToIndex the index (exclusive) of the last element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3095
     *                 first array to be tested
59055
57ad70bcf06c 8233731: repeated typo "fro" for "for"
coffeys
parents: 59042
diff changeset
  3096
     * @param b the second array to be tested for equality
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3097
     * @param bFromIndex the index (inclusive) of the first element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3098
     *                   second array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3099
     * @param bToIndex the index (exclusive) of the last element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3100
     *                 second array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3101
     * @param cmp the comparator to compare array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3102
     * @param <T> the type of array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3103
     * @return {@code true} if the two arrays, over the specified ranges, are
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3104
     *         equal
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3105
     * @throws IllegalArgumentException
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3106
     *         if {@code aFromIndex > aToIndex} or
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3107
     *         if {@code bFromIndex > bToIndex}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3108
     * @throws ArrayIndexOutOfBoundsException
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3109
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3110
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3111
     * @throws NullPointerException
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3112
     *         if either array or the comparator is {@code null}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3113
     * @since 9
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3114
     */
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3115
    public static <T> boolean equals(T[] a, int aFromIndex, int aToIndex,
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3116
                                     T[] b, int bFromIndex, int bToIndex,
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3117
                                     Comparator<? super T> cmp) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3118
        Objects.requireNonNull(cmp);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3119
        rangeCheck(a.length, aFromIndex, aToIndex);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3120
        rangeCheck(b.length, bFromIndex, bToIndex);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3121
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3122
        int aLength = aToIndex - aFromIndex;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3123
        int bLength = bToIndex - bFromIndex;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3124
        if (aLength != bLength)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3125
            return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3126
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3127
        for (int i = 0; i < aLength; i++) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3128
            if (cmp.compare(a[aFromIndex++], b[bFromIndex++]) != 0)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3129
                return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3130
        }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3131
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3132
        return true;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3133
    }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3134
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
    // Filling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
     * Assigns the specified long value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
     * of longs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
    public static void fill(long[] a, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
     * Assigns the specified long value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
     * 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
  3152
     * 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
  3153
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
     * @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
  3162
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3163
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3164
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
    public static void fill(long[] a, int fromIndex, int toIndex, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
     * Assigns the specified int value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
     * of ints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
    public static void fill(int[] a, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
     * Assigns the specified int value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
     * 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
  3187
     * 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
  3188
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
     * @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
  3197
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3198
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3199
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
    public static void fill(int[] a, int fromIndex, int toIndex, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
     * Assigns the specified short value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
     * of shorts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
    public static void fill(short[] a, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
     * Assigns the specified short value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
     * 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
  3222
     * 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
  3223
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
     * @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
  3232
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3233
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3234
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
    public static void fill(short[] a, int fromIndex, int toIndex, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
     * Assigns the specified char value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
     * of chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
    public static void fill(char[] a, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
     * Assigns the specified char value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
     * 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
  3257
     * 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
  3258
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
     * @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
  3267
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3268
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3269
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
    public static void fill(char[] a, int fromIndex, int toIndex, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
     * Assigns the specified byte value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
     * of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
    public static void fill(byte[] a, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
     * Assigns the specified byte value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
     * 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
  3292
     * 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
  3293
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
     * @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
  3302
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3303
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3304
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
    public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
     * Assigns the specified boolean value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
     * array of booleans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
    public static void fill(boolean[] a, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
     * Assigns the specified boolean value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
     * 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
  3327
     * 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
  3328
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
     * @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
  3337
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3338
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3339
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
    public static void fill(boolean[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
                            boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
     * Assigns the specified double value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
     * array of doubles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
    public static void fill(double[] a, double val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
     * Assigns the specified double value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
     * 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
  3363
     * 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
  3364
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
     * @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
  3373
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3374
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3375
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
    public static void fill(double[] a, int fromIndex, int toIndex,double val){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
     * Assigns the specified float value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
     * of floats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
    public static void fill(float[] a, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
     * Assigns the specified float value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
     * 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
  3398
     * 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
  3399
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
     * @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
  3408
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3409
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3410
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
    public static void fill(float[] a, int fromIndex, int toIndex, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
     * Assigns the specified Object reference to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
     * array of Objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
     * @throws ArrayStoreException if the specified value is not of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
     *         runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
    public static void fill(Object[] a, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
     * Assigns the specified Object reference to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
     * 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
  3435
     * 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
  3436
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
     * @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
  3445
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3446
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3447
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
     * @throws ArrayStoreException if the specified value is not of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
     *         runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
    public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  3457
    // Cloning
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
     * Copies the specified array, truncating or padding with nulls (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
     * 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
  3464
     * copy but not the original, the copy will contain {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
     * The resulting array is of exactly the same class as the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3469
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
     * @return a copy of the original array, truncated or padded with nulls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3474
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3475
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3478
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
    public static <T> T[] copyOf(T[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
        return (T[]) copyOf(original, newLength, original.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
     * Copies the specified array, truncating or padding with nulls (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
     * 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
  3488
     * copy but not the original, the copy will contain {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
     * 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
  3491
     * The resulting array is of the class {@code newType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3493
     * @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
  3494
     * @param <T> the class of the objects in the returned array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
     * @param newType the class of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
     * @return a copy of the original array, truncated or padded with nulls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3500
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3501
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
     * @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
  3503
     *     {@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
  3504
     *     an array of class {@code newType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29223
diff changeset
  3507
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
    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
  3509
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
        T[] copy = ((Object)newType == (Object)Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
            ? (T[]) new Object[newLength]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
     * 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
  3523
     * copy but not the original, the copy will contain {@code (byte)0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3531
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3532
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
    public static byte[] copyOf(byte[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
        byte[] copy = new byte[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
     * 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
  3547
     * copy but not the original, the copy will contain {@code (short)0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3555
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3556
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
    public static short[] copyOf(short[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
        short[] copy = new short[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
     * 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
  3571
     * copy but not the original, the copy will contain {@code 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3579
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3580
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
    public static int[] copyOf(int[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
        int[] copy = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
     * 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
  3595
     * copy but not the original, the copy will contain {@code 0L}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3603
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3604
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
    public static long[] copyOf(long[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
        long[] copy = new long[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
     * Copies the specified array, truncating or padding with null characters (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
     * so the copy has the specified length.  For all indices that are valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
     * in both the original array and the copy, the two arrays will contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
     * 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
  3619
     * the original, the copy will contain {@code '\\u000'}.  Such indices
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
     * will exist if and only if the specified length is greater than that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
     * the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
     * @return a copy of the original array, truncated or padded with null characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3627
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3628
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
    public static char[] copyOf(char[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
        char[] copy = new char[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
     * 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
  3643
     * copy but not the original, the copy will contain {@code 0f}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3651
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3652
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
    public static float[] copyOf(float[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
        float[] copy = new float[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
     * 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
  3667
     * copy but not the original, the copy will contain {@code 0d}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3675
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3676
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
    public static double[] copyOf(double[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
        double[] copy = new double[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3687
     * Copies the specified array, truncating or padding with {@code false} (if necessary)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
     * 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
  3691
     * copy but not the original, the copy will contain {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
     * @return a copy of the original array, truncated or padded with false elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3699
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3700
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
    public static boolean[] copyOf(boolean[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
        boolean[] copy = new boolean[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
     * 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
  3712
     * 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
  3713
     * 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
  3714
     * {@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
  3715
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
     * 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
  3718
     * ({@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
  3719
     * 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
  3720
     * {@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
  3721
     * 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
  3722
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
     * The resulting array is of exactly the same class as the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3726
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
     *     truncated or padded with nulls to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3735
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3736
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3739
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
    public static <T> T[] copyOfRange(T[] original, int from, int to) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3741
        return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
     * 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
  3746
     * 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
  3747
     * 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
  3748
     * {@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
  3749
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
     * 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
  3752
     * ({@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
  3753
     * 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
  3754
     * {@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
  3755
     * 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
  3756
     * 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
  3757
     * The resulting array is of the class {@code newType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3759
     * @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
  3760
     * @param <T> the class of the objects in the returned array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
     * @param newType the class of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
     *     truncated or padded with nulls to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3770
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3771
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
     * @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
  3773
     *     {@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
  3774
     *     an array of class {@code newType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29223
diff changeset
  3777
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
    public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
            throw new IllegalArgumentException(from + " > " + to);
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3782
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
        T[] copy = ((Object)newType == (Object)Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
            ? (T[]) new Object[newLength]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
     * 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
  3793
     * 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
  3794
     * 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
  3795
     * {@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
  3796
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
     * 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
  3799
     * ({@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
  3800
     * 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
  3801
     * {@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
  3802
     * 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
  3803
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3813
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3814
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
    public static byte[] copyOfRange(byte[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
        byte[] copy = new byte[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
     * 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
  3829
     * 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
  3830
     * 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
  3831
     * {@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
  3832
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
     * 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
  3835
     * ({@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
  3836
     * 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
  3837
     * {@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
  3838
     * 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
  3839
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3849
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3850
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
    public static short[] copyOfRange(short[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
        short[] copy = new short[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
     * 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
  3865
     * 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
  3866
     * 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
  3867
     * {@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
  3868
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
     * 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
  3871
     * ({@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
  3872
     * 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
  3873
     * {@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
  3874
     * 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
  3875
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3885
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3886
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
    public static int[] copyOfRange(int[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
        int[] copy = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
     * 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
  3901
     * 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
  3902
     * 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
  3903
     * {@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
  3904
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
     * 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
  3907
     * ({@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
  3908
     * 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
  3909
     * {@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
  3910
     * 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
  3911
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3921
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3922
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
    public static long[] copyOfRange(long[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
        long[] copy = new long[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
     * 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
  3937
     * 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
  3938
     * 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
  3939
     * {@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
  3940
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
     * 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
  3943
     * ({@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
  3944
     * 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
  3945
     * {@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
  3946
     * 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
  3947
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
     *     truncated or padded with null characters to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3956
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3957
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3958
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
    public static char[] copyOfRange(char[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
        char[] copy = new char[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
     * 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
  3973
     * 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
  3974
     * 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
  3975
     * {@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
  3976
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
     * 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
  3979
     * ({@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
  3980
     * 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
  3981
     * {@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
  3982
     * 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
  3983
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3993
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3994
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
    public static float[] copyOfRange(float[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4001
        float[] copy = new float[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4002
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4003
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4004
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4007
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4008
     * 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
  4009
     * 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
  4010
     * 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
  4011
     * {@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
  4012
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4013
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
     * 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
  4015
     * ({@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
  4016
     * 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
  4017
     * {@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
  4018
     * 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
  4019
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4022
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4023
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4024
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4029
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4030
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
    public static double[] copyOfRange(double[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
        double[] copy = new double[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4038
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4039
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4040
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4043
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4044
     * 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
  4045
     * 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
  4046
     * 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
  4047
     * {@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
  4048
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4049
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
     * 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
  4051
     * ({@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
  4052
     * 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
  4053
     * {@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
  4054
     * 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
  4055
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4058
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4059
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
     *     truncated or padded with false elements to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4065
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4066
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
    public static boolean[] copyOfRange(boolean[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
        boolean[] copy = new boolean[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4074
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4075
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4076
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
    // Misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4081
    /**
51884
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4082
     * Returns a fixed-size list backed by the specified array. Changes made to
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4083
     * the array will be visible in the returned list, and changes made to the
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4084
     * list will be visible in the array. The returned list is
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4085
     * {@link Serializable} and implements {@link RandomAccess}.
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4086
     *
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4087
     * <p>The returned list implements the optional {@code Collection} methods, except
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4088
     * those that would change the size of the returned list. Those methods leave
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4089
     * the list unchanged and throw {@link UnsupportedOperationException}.
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4090
     *
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4091
     * @apiNote
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4092
     * This method acts as bridge between array-based and collection-based
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4093
     * APIs, in combination with {@link Collection#toArray}.
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4094
     *
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4095
     * <p>This method provides a way to wrap an existing array:
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4096
     * <pre>{@code
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4097
     *     Integer[] numbers = ...
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4098
     *     ...
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4099
     *     List<Integer> values = Arrays.asList(numbers);
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4100
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
     * <p>This method also provides a convenient way to create a fixed-size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
     * list initialized to contain several elements:
51884
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4104
     * <pre>{@code
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4105
     *     List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4106
     * }</pre>
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4107
     *
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4108
     * <p><em>The list returned by this method is modifiable.</em>
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4109
     * To create an unmodifiable list, use
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4110
     * {@link Collections#unmodifiableList Collections.unmodifiableList}
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4111
     * or <a href="List.html#unmodifiable">Unmodifiable Lists</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4112
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4113
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4114
     * @param a the array by which the list will be backed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
     * @return a list view of the specified array
51884
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4116
     * @throws NullPointerException if the specified array is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4117
     */
8151
88b01a6d5f51 7006578: Project Coin: Retrofit JDK libraries with @SafeVarargs
darcy
parents: 7816
diff changeset
  4118
    @SafeVarargs
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  4119
    @SuppressWarnings("varargs")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4120
    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
  4121
        return new ArrayList<>(a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
    private static class ArrayList<E> extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
        implements RandomAccess, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
    {
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 51884
diff changeset
  4130
        @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4131
        private static final long serialVersionUID = -2764017481108945198L;
58520
e036ee8bae56 8231202: Suppress warnings on non-serializable non-transient instance fields in serializable classes
darcy
parents: 57956
diff changeset
  4132
        @SuppressWarnings("serial") // Conditionally serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
        private final E[] a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
        ArrayList(E[] array) {
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4136
            a = Objects.requireNonNull(array);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4139
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
            return a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4144
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4145
        public Object[] toArray() {
31540
6efd719b3330 6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
martin
parents: 29223
diff changeset
  4146
            return Arrays.copyOf(a, a.length, Object[].class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4148
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4149
        @Override
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  4150
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4151
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4152
            int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4153
            if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4154
                return Arrays.copyOf(this.a, size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4155
                                     (Class<? extends T[]>) a.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4156
            System.arraycopy(this.a, 0, a, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4157
            if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
                a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4162
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
            return a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4166
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4167
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
        public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
            E oldValue = a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
            a[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4171
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4174
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
        public int indexOf(Object o) {
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4176
            E[] a = this.a;
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4177
            if (o == null) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4178
                for (int i = 0; i < a.length; i++)
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4179
                    if (a[i] == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4181
            } else {
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4182
                for (int i = 0; i < a.length; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4183
                    if (o.equals(a[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4184
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4186
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4188
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4189
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4190
        public boolean contains(Object o) {
26451
f86e59f18322 8056951: pico-optimize contains(Object) methods
martin
parents: 25859
diff changeset
  4191
            return indexOf(o) >= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4192
        }
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4193
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4194
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4195
        public Spliterator<E> spliterator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4196
            return Spliterators.spliterator(a, Spliterator.ORDERED);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4197
        }
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4198
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4199
        @Override
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4200
        public void forEach(Consumer<? super E> action) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4201
            Objects.requireNonNull(action);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4202
            for (E e : a) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4203
                action.accept(e);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4204
            }
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4205
        }
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4206
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4207
        @Override
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4208
        public void replaceAll(UnaryOperator<E> operator) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4209
            Objects.requireNonNull(operator);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4210
            E[] a = this.a;
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4211
            for (int i = 0; i < a.length; i++) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4212
                a[i] = operator.apply(a[i]);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4213
            }
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4214
        }
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4215
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4216
        @Override
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4217
        public void sort(Comparator<? super E> c) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4218
            Arrays.sort(a, c);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4219
        }
37685
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4220
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4221
        @Override
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4222
        public Iterator<E> iterator() {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4223
            return new ArrayItr<>(a);
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4224
        }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4225
    }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4226
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4227
    private static class ArrayItr<E> implements Iterator<E> {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4228
        private int cursor;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4229
        private final E[] a;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4230
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4231
        ArrayItr(E[] a) {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4232
            this.a = a;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4233
        }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4234
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4235
        @Override
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4236
        public boolean hasNext() {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4237
            return cursor < a.length;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4238
        }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4239
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4240
        @Override
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4241
        public E next() {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4242
            int i = cursor;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4243
            if (i >= a.length) {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4244
                throw new NoSuchElementException();
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4245
            }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4246
            cursor = i + 1;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4247
            return a[i];
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4248
        }
2
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
     * 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
  4253
     * 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
  4254
     * 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
  4255
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4257
     * <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
  4258
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4259
     * 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
  4260
     * 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
  4261
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4263
     * @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
  4264
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4265
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4267
    public static int hashCode(long a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4268
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4269
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4271
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4272
        for (long element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4273
            int elementHash = (int)(element ^ (element >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4274
            result = 31 * result + elementHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4277
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4281
     * 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
  4282
     * 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
  4283
     * 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
  4284
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4286
     * <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
  4287
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4288
     * 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
  4289
     * 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
  4290
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4292
     * @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
  4293
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4294
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4296
    public static int hashCode(int a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4297
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4298
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4300
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4301
        for (int element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4302
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4304
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4308
     * 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
  4309
     * 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
  4310
     * 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
  4311
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4313
     * <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
  4314
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4315
     * 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
  4316
     * 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
  4317
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4319
     * @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
  4320
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4321
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4323
    public static int hashCode(short a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4324
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4325
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4327
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4328
        for (short element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4329
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4331
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4335
     * 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
  4336
     * 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
  4337
     * 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
  4338
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4340
     * <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
  4341
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4342
     * 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
  4343
     * 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
  4344
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4346
     * @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
  4347
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4348
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4350
    public static int hashCode(char a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4351
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4352
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4354
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4355
        for (char element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4356
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4358
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4362
     * 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
  4363
     * 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
  4364
     * 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
  4365
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4367
     * <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
  4368
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4369
     * 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
  4370
     * 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
  4371
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4373
     * @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
  4374
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4375
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4377
    public static int hashCode(byte a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4378
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4379
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4381
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4382
        for (byte element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4383
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4385
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4389
     * 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
  4390
     * 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
  4391
     * 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
  4392
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4394
     * <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
  4395
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4396
     * 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
  4397
     * 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
  4398
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4400
     * @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
  4401
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4402
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4404
    public static int hashCode(boolean a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4405
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4406
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4408
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4409
        for (boolean element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4410
            result = 31 * result + (element ? 1231 : 1237);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4412
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4416
     * 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
  4417
     * 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
  4418
     * 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
  4419
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4421
     * <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
  4422
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4423
     * 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
  4424
     * 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
  4425
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4427
     * @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
  4428
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4429
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4431
    public static int hashCode(float a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4432
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4433
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4435
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4436
        for (float element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4437
            result = 31 * result + Float.floatToIntBits(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4439
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4443
     * 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
  4444
     * 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
  4445
     * 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
  4446
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4448
     * <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
  4449
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4450
     * 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
  4451
     * 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
  4452
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4454
     * @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
  4455
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4456
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4458
    public static int hashCode(double a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4459
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4460
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4462
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4463
        for (double element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4464
            long bits = Double.doubleToLongBits(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4465
            result = 31 * result + (int)(bits ^ (bits >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4467
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4471
     * Returns a hash code based on the contents of the specified array.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4472
     * the array contains other arrays as elements, the hash code is based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4473
     * their identities rather than their contents.  It is therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4474
     * acceptable to invoke this method on an array that contains itself as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4475
     * element,  either directly or indirectly through one or more levels of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4476
     * arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4477
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4478
     * <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
  4479
     * {@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
  4480
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4482
     * <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
  4483
     * 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
  4484
     * is {@code null}, in which case {@code 0} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4485
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4486
     * @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
  4487
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4488
     * @see #deepHashCode(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4489
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4491
    public static int hashCode(Object a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4492
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4493
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4495
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4497
        for (Object element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4498
            result = 31 * result + (element == null ? 0 : element.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4500
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4504
     * Returns a hash code based on the "deep contents" of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4505
     * array.  If the array contains other arrays as elements, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4506
     * hash code is based on their contents and so on, ad infinitum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4507
     * It is therefore unacceptable to invoke this method on an array that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4508
     * contains itself as an element, either directly or indirectly through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4509
     * one or more levels of arrays.  The behavior of such an invocation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4510
     * undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4511
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4512
     * <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
  4513
     * {@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
  4514
     * {@code Arrays.deepHashCode(a) == Arrays.deepHashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4516
     * <p>The computation of the value returned by this method is similar to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4517
     * 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
  4518
     * 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
  4519
     * 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
  4520
     * 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
  4521
     * 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
  4522
     * 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
  4523
     * {@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
  4524
     * of a reference type.  If {@code a} is {@code null}, this method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4525
     * returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4527
     * @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
  4528
     * @return a deep-content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4529
     * @see #hashCode(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4530
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4532
    public static int deepHashCode(Object a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4533
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4534
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4536
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4538
        for (Object element : a) {
49955
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4539
            final int elementHash;
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4540
            final Class<?> cl;
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4541
            if (element == null)
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4542
                elementHash = 0;
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4543
            else if ((cl = element.getClass().getComponentType()) == null)
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4544
                elementHash = element.hashCode();
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4545
            else if (element instanceof Object[])
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4546
                elementHash = deepHashCode((Object[]) element);
49955
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4547
            else
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4548
                elementHash = primitiveArrayHashCode(element, cl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4550
            result = 31 * result + elementHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4553
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4555
49955
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4556
    private static int primitiveArrayHashCode(Object a, Class<?> cl) {
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4557
        return
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4558
            (cl == byte.class)    ? hashCode((byte[]) a)    :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4559
            (cl == int.class)     ? hashCode((int[]) a)     :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4560
            (cl == long.class)    ? hashCode((long[]) a)    :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4561
            (cl == char.class)    ? hashCode((char[]) a)    :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4562
            (cl == short.class)   ? hashCode((short[]) a)   :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4563
            (cl == boolean.class) ? hashCode((boolean[]) a) :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4564
            (cl == double.class)  ? hashCode((double[]) a)  :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4565
            // If new primitive types are ever added, this method must be
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4566
            // expanded or we will fail here with ClassCastException.
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4567
            hashCode((float[]) a);
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4568
    }
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4569
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4570
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4571
     * Returns {@code true} if the two specified arrays are <i>deeply
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4572
     * equal</i> to one another.  Unlike the {@link #equals(Object[],Object[])}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4573
     * method, this method is appropriate for use with nested arrays of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4574
     * arbitrary depth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4576
     * <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
  4577
     * are {@code null}, or if they refer to arrays that contain the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4578
     * number of elements and all corresponding pairs of elements in the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4579
     * arrays are deeply equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4580
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4581
     * <p>Two possibly {@code null} elements {@code e1} and {@code e2} are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4582
     * deeply equal if any of the following conditions hold:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4583
     * <ul>
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4584
     *    <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
  4585
     *         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
  4586
     *    <li> {@code e1} and {@code e2} are arrays of the same primitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4587
     *         type, and the appropriate overloading of
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4588
     *         {@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
  4589
     *    <li> {@code e1 == e2}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4590
     *    <li> {@code e1.equals(e2)} would return true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4591
     * </ul>
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4592
     * Note that this definition permits {@code null} elements at any depth.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4594
     * <p>If either of the specified arrays contain themselves as elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4595
     * either directly or indirectly through one or more levels of arrays,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4596
     * the behavior of this method is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4598
     * @param a1 one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4599
     * @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
  4600
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4601
     * @see #equals(Object[],Object[])
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4602
     * @see Objects#deepEquals(Object, Object)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4603
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4605
    public static boolean deepEquals(Object[] a1, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4606
        if (a1 == a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4607
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4608
        if (a1 == null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4609
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4610
        int length = a1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4611
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4612
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4614
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4615
            Object e1 = a1[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4616
            Object e2 = a2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4618
            if (e1 == e2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4619
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4620
            if (e1 == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4621
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4623
            // 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
  4624
            boolean eq = deepEquals0(e1, e2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4626
            if (!eq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4627
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4629
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4631
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4632
    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
  4633
        assert e1 != null;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4634
        boolean eq;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4635
        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
  4636
            eq = deepEquals ((Object[]) e1, (Object[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4637
        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
  4638
            eq = equals((byte[]) e1, (byte[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4639
        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
  4640
            eq = equals((short[]) e1, (short[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4641
        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
  4642
            eq = equals((int[]) e1, (int[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4643
        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
  4644
            eq = equals((long[]) e1, (long[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4645
        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
  4646
            eq = equals((char[]) e1, (char[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4647
        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
  4648
            eq = equals((float[]) e1, (float[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4649
        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
  4650
            eq = equals((double[]) e1, (double[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4651
        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
  4652
            eq = equals((boolean[]) e1, (boolean[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4653
        else
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4654
            eq = e1.equals(e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4655
        return eq;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4656
    }
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4657
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4659
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4660
     * 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
  4661
     * 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
  4662
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4663
     * 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
  4664
     * {@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
  4665
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4667
     * @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
  4668
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4669
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4671
    public static String toString(long[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4672
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4673
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4674
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4675
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4676
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4678
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4679
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4680
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4681
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4682
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4683
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4684
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4688
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4689
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4690
     * 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
  4691
     * 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
  4692
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4693
     * 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
  4694
     * {@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
  4695
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4697
     * @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
  4698
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4699
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4701
    public static String toString(int[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4702
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4703
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4704
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4705
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4706
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4708
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4709
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4710
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4711
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4712
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4713
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4714
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4719
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4720
     * 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
  4721
     * 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
  4722
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4723
     * 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
  4724
     * {@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
  4725
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4727
     * @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
  4728
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4729
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4731
    public static String toString(short[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4732
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4733
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4734
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4735
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4736
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4737
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4738
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4739
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4740
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4741
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4742
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4743
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4744
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4749
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4750
     * 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
  4751
     * 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
  4752
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4753
     * 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
  4754
     * {@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
  4755
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4756
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4757
     * @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
  4758
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4759
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4760
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4761
    public static String toString(char[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4762
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4763
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4764
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4765
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4766
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4768
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4769
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4770
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4771
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4772
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4773
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4774
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4778
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4779
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4780
     * 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
  4781
     * enclosed in square brackets ({@code "[]"}).  Adjacent elements
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4782
     * are separated by the characters {@code ", "} (a comma followed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4783
     * 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
  4784
     * {@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
  4785
     * {@code a} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4787
     * @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
  4788
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4789
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4790
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4791
    public static String toString(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4792
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4793
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4794
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4795
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4796
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4798
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4799
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4800
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4801
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4802
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4803
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4804
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4808
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4809
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4810
     * 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
  4811
     * 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
  4812
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4813
     * 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
  4814
     * {@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
  4815
     * {@code a} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4816
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4817
     * @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
  4818
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4819
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4820
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4821
    public static String toString(boolean[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4822
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4823
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4824
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4825
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4826
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4828
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4829
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4830
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4831
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4832
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4833
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4834
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4838
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4839
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4840
     * 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
  4841
     * 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
  4842
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4843
     * 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
  4844
     * {@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
  4845
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4846
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4847
     * @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
  4848
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4849
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4850
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4851
    public static String toString(float[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4852
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4853
            return "null";
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  4854
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4855
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4856
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4857
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4859
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4860
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4861
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4862
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4863
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4864
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4865
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4868
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4869
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4870
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4871
     * 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
  4872
     * 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
  4873
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4874
     * 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
  4875
     * {@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
  4876
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4877
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4878
     * @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
  4879
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4880
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4881
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4882
    public static String toString(double[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4883
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4884
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4885
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4886
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4887
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4889
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4890
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4891
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4892
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4893
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4894
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4895
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4899
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4900
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4901
     * If the array contains other arrays as elements, they are converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4902
     * 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
  4903
     * {@code Object}, which describes their <i>identities</i> rather than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4904
     * their contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4905
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4906
     * <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
  4907
     * 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
  4908
     * is {@code null}, in which case {@code "null"} is returned.
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
     * @see #deepToString(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4913
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4914
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4915
    public static String toString(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4916
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4917
            return "null";
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  4918
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4919
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4920
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4921
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4923
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4924
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4925
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4926
            b.append(String.valueOf(a[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4927
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4928
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4929
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4933
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4934
     * Returns a string representation of the "deep contents" of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4935
     * array.  If the array contains other arrays as elements, the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4936
     * representation contains their contents and so on.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4937
     * designed for converting multidimensional arrays to strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4938
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4939
     * <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
  4940
     * elements, enclosed in square brackets ({@code "[]"}).  Adjacent
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4941
     * elements are separated by the characters {@code ", "} (a comma
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4942
     * 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
  4943
     * {@code String.valueOf(Object)}, unless they are themselves
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4944
     * arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4945
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4946
     * <p>If an element {@code e} is an array of a primitive type, it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4947
     * 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
  4948
     * {@code Arrays.toString(e)}.  If an element {@code e} is an array of a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4949
     * reference type, it is converted to a string as by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4950
     * this method recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4951
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4952
     * <p>To avoid infinite recursion, if the specified array contains itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4953
     * as an element, or contains an indirect reference to itself through one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4954
     * 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
  4955
     * {@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
  4956
     * to itself would be rendered as {@code "[[...]]"}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4957
     *
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4958
     * <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
  4959
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4960
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4961
     * @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
  4962
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4963
     * @see #toString(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4964
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4965
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4966
    public static String deepToString(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4967
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4968
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4970
        int bufLen = 20 * a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4971
        if (a.length != 0 && bufLen <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4972
            bufLen = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4973
        StringBuilder buf = new StringBuilder(bufLen);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  4974
        deepToString(a, buf, new HashSet<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4975
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4976
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4978
    private static void deepToString(Object[] a, StringBuilder buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4979
                                     Set<Object[]> dejaVu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4980
        if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4981
            buf.append("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4982
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4984
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4985
        if (iMax == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4986
            buf.append("[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4987
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4990
        dejaVu.add(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4991
        buf.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4992
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4993
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4994
            Object element = a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4995
            if (element == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4996
                buf.append("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4997
            } else {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  4998
                Class<?> eClass = element.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5000
                if (eClass.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5001
                    if (eClass == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5002
                        buf.append(toString((byte[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5003
                    else if (eClass == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5004
                        buf.append(toString((short[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5005
                    else if (eClass == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5006
                        buf.append(toString((int[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5007
                    else if (eClass == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5008
                        buf.append(toString((long[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5009
                    else if (eClass == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5010
                        buf.append(toString((char[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5011
                    else if (eClass == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5012
                        buf.append(toString((float[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5013
                    else if (eClass == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5014
                        buf.append(toString((double[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5015
                    else if (eClass == boolean[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5016
                        buf.append(toString((boolean[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5017
                    else { // element is an array of object references
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5018
                        if (dejaVu.contains(element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5019
                            buf.append("[...]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5020
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5021
                            deepToString((Object[])element, buf, dejaVu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5022
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5023
                } else {  // element is non-null and not an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5024
                    buf.append(element.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5025
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5026
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5027
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5028
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5029
            buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5031
        buf.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5032
        dejaVu.remove(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5033
    }
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5034
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5035
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5036
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5037
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5038
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5039
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5040
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5041
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5042
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5043
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5044
     * 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
  5045
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5046
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5047
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5048
     *          .forEach(i -> array[i] = generator.apply(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5049
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5050
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5051
     * @param <T> type of elements of the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5052
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5053
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5054
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5055
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5056
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5057
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5058
    public static <T> void setAll(T[] array, IntFunction<? extends T> generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5059
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5060
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5061
            array[i] = generator.apply(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5062
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5063
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5064
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5065
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5066
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5067
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5068
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5069
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5070
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5071
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5072
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5073
     * 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
  5074
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5075
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5076
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5077
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5078
     *          .forEach(i -> array[i] = generator.apply(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5079
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5080
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5081
     * @param <T> type of elements of the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5082
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5083
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5084
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5085
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5086
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5087
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5088
    public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5089
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5090
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5091
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5092
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5093
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5094
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5095
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5096
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5097
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5098
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5099
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5100
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5101
     * 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
  5102
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5103
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5104
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5105
     *          .forEach(i -> array[i] = generator.applyAsInt(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5106
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5107
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5108
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5109
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5110
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5111
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5112
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5113
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5114
    public static void setAll(int[] array, IntUnaryOperator generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5115
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5116
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5117
            array[i] = generator.applyAsInt(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5118
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5119
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5120
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5121
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5122
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5123
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5124
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5125
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5126
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5127
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5128
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5129
     * 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
  5130
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5131
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5132
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5133
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5134
     *          .forEach(i -> array[i] = generator.applyAsInt(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5135
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5136
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5137
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5138
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5139
     * value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5140
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5141
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5142
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5143
    public static void parallelSetAll(int[] array, IntUnaryOperator generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5144
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5145
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5146
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5147
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5148
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5149
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5150
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5151
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5152
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5153
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5154
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5155
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5156
     * 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
  5157
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5158
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5159
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5160
     *          .forEach(i -> array[i] = generator.applyAsLong(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5161
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5162
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5163
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5164
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5165
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5166
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5167
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5168
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5169
    public static void setAll(long[] array, IntToLongFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5170
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5171
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5172
            array[i] = generator.applyAsLong(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5173
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5174
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5175
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5176
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5177
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5178
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5179
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5180
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5181
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5182
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5183
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5184
     * 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
  5185
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5186
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5187
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5188
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5189
     *          .forEach(i -> array[i] = generator.applyAsLong(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5190
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5191
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5192
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5193
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5194
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5195
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5196
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5197
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5198
    public static void parallelSetAll(long[] array, IntToLongFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5199
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5200
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5201
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5202
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5203
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5204
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5205
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5206
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5207
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5208
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5209
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5210
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5211
     * 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
  5212
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5213
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5214
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5215
     *          .forEach(i -> array[i] = generator.applyAsDouble(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5216
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5217
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5218
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5219
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5220
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5221
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5222
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5223
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5224
    public static void setAll(double[] array, IntToDoubleFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5225
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5226
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5227
            array[i] = generator.applyAsDouble(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5228
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5229
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5230
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5231
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5232
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5233
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5234
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5235
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5236
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5237
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5238
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5239
     * 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
  5240
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5241
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5242
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5243
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5244
     *          .forEach(i -> array[i] = generator.applyAsDouble(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5245
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5246
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5247
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5248
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5249
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5250
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5251
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5252
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5253
    public static void parallelSetAll(double[] array, IntToDoubleFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5254
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5255
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5256
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5257
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5258
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5259
     * Returns a {@link Spliterator} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5260
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5261
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5262
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5263
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5264
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5265
     * @param <T> type of elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5266
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5267
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5268
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5269
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5270
    public static <T> Spliterator<T> spliterator(T[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5271
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5272
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5273
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5274
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5275
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5276
     * Returns a {@link Spliterator} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5277
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5278
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5279
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5280
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5281
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5282
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5283
     * @param <T> type of elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5284
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5285
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5286
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5287
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5288
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5289
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5290
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5291
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5292
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5293
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5294
    public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5295
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5296
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5297
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5298
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5299
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5300
     * Returns a {@link Spliterator.OfInt} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5301
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5302
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5303
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5304
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5305
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5306
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5307
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5308
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5309
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5310
    public static Spliterator.OfInt spliterator(int[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5311
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5312
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5313
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5314
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5315
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5316
     * Returns a {@link Spliterator.OfInt} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5317
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5318
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5319
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5320
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5321
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5322
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5323
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5324
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5325
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5326
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5327
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5328
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5329
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5330
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5331
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5332
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5333
    public static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5334
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5335
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5336
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5337
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5338
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5339
     * Returns a {@link Spliterator.OfLong} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5340
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5341
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5342
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5343
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5344
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5345
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5346
     * @return the spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5347
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5348
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5349
    public static Spliterator.OfLong spliterator(long[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5350
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5351
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5352
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5353
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5354
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5355
     * Returns a {@link Spliterator.OfLong} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5356
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5357
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5358
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5359
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5360
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5361
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5362
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5363
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5364
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5365
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5366
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5367
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5368
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5369
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5370
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5371
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5372
    public static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5373
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5374
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5375
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5376
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5377
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5378
     * Returns a {@link Spliterator.OfDouble} covering all of the specified
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5379
     * array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5380
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5381
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5382
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5383
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5384
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5385
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5386
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5387
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5388
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5389
    public static Spliterator.OfDouble spliterator(double[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5390
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5391
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5392
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5393
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5394
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5395
     * Returns a {@link Spliterator.OfDouble} covering the specified range of
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5396
     * the specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5397
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5398
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5399
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5400
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5401
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5402
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5403
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5404
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5405
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5406
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5407
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5408
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5409
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5410
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5411
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5412
    public static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5413
        return Spliterators.spliterator(array, startInclusive, endExclusive,
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5414
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5415
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5416
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5417
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5418
     * Returns a sequential {@link Stream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5419
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5420
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5421
     * @param <T> The type of the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5422
     * @param array The array, assumed to be unmodified during use
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5423
     * @return a {@code Stream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5424
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5425
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5426
    public static <T> Stream<T> stream(T[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5427
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5428
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5429
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5430
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5431
     * Returns a sequential {@link Stream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5432
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5433
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5434
     * @param <T> the type of the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5435
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5436
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5437
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5438
     * @return a {@code Stream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5439
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5440
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5441
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5442
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5443
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5444
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5445
    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
  5446
        return StreamSupport.stream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5447
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5448
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5449
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5450
     * Returns a sequential {@link IntStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5451
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5452
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5453
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5454
     * @return an {@code IntStream} for the array
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5455
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5456
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5457
    public static IntStream stream(int[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5458
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5459
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5460
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5461
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5462
     * Returns a sequential {@link IntStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5463
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5464
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5465
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5466
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5467
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5468
     * @return an {@code IntStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5469
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5470
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5471
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5472
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5473
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5474
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5475
    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
  5476
        return StreamSupport.intStream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5477
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5478
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5479
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5480
     * Returns a sequential {@link LongStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5481
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5482
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5483
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5484
     * @return a {@code LongStream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5485
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5486
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5487
    public static LongStream stream(long[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5488
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5489
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5490
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5491
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5492
     * Returns a sequential {@link LongStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5493
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5494
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5495
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5496
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5497
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5498
     * @return a {@code LongStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5499
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5500
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5501
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5502
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5503
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5504
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5505
    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
  5506
        return StreamSupport.longStream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5507
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5508
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5509
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5510
     * Returns a sequential {@link DoubleStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5511
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5512
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5513
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5514
     * @return a {@code DoubleStream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5515
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5516
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5517
    public static DoubleStream stream(double[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5518
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5519
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5520
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5521
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5522
     * Returns a sequential {@link DoubleStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5523
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5524
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5525
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5526
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5527
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5528
     * @return a {@code DoubleStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5529
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5530
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5531
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5532
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5533
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5534
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5535
    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
  5536
        return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5537
    }
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5538
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5539
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5540
    // Comparison methods
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5541
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5542
    // Compare boolean
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5543
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5544
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5545
     * Compares two {@code boolean} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5546
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5547
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5548
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5549
     * {@link Boolean#compare(boolean, boolean)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5550
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5551
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5552
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5553
     * (See {@link #mismatch(boolean[], boolean[])} for the definition of a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5554
     * common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5555
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5556
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5557
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5558
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5559
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5560
     * <p>The comparison is consistent with {@link #equals(boolean[], boolean[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5561
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5562
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5563
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5564
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5565
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5566
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5567
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5568
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5569
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5570
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5571
     *         return Boolean.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5572
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5573
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5574
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5575
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5576
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5577
     * @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
  5578
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5579
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5580
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5581
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5582
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5583
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5584
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5585
    public static int compare(boolean[] a, boolean[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5586
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5587
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5588
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5589
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5590
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5591
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5592
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5593
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5594
            return Boolean.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5595
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5596
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5597
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5598
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5599
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5600
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5601
     * Compares two {@code boolean} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5602
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5603
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5604
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5605
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5606
     * elements, as if by {@link Boolean#compare(boolean, boolean)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5607
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5608
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5609
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5610
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5611
     * (See {@link #mismatch(boolean[], int, int, boolean[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5612
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5613
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5614
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5615
     * {@link #equals(boolean[], int, int, boolean[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5616
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5617
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5618
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5619
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5620
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5621
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5622
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5623
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5624
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5625
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5626
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5627
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5628
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5629
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5630
     *         return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5631
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5632
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5633
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5634
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5635
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5636
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5637
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5638
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5639
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5640
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5641
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5642
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5643
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5644
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5645
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5646
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5647
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5648
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5649
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5650
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5651
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5652
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5653
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5654
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5655
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5656
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5657
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5658
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5659
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5660
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5661
    public static int compare(boolean[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5662
                              boolean[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5663
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5664
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5665
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5666
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5667
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5668
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5669
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5670
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5671
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5672
            return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5673
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5674
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5675
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5676
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5677
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5678
    // Compare byte
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5679
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5680
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5681
     * Compares two {@code byte} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5682
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5683
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5684
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5685
     * {@link Byte#compare(byte, byte)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5686
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5687
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5688
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5689
     * (See {@link #mismatch(byte[], byte[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5690
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5691
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5692
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5693
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5694
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5695
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5696
     * <p>The comparison is consistent with {@link #equals(byte[], byte[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5697
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5698
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5699
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5700
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5701
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5702
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5703
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5704
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5705
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5706
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5707
     *         return Byte.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5708
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5709
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5710
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5711
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5712
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5713
     * @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
  5714
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5715
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5716
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5717
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5718
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5719
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5720
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5721
    public static int compare(byte[] a, byte[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5722
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5723
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5724
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5725
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5726
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5727
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5728
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5729
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5730
            return Byte.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5731
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5732
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5733
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5734
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5735
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5736
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5737
     * Compares two {@code byte} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5738
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5739
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5740
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5741
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5742
     * elements, as if by {@link Byte#compare(byte, byte)}, at a relative index
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5743
     * within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5744
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5745
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5746
     * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5747
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5748
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5749
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5750
     * {@link #equals(byte[], int, int, byte[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5751
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5752
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5753
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5754
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5755
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5756
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5757
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5758
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5759
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5760
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5761
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5762
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5763
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5764
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5765
     *         return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5766
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5767
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5768
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5769
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5770
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5771
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5772
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5773
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5774
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5775
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5776
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5777
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5778
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5779
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5780
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5781
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5782
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5783
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5784
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5785
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5786
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5787
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5788
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5789
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5790
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5791
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5792
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5793
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5794
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5795
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5796
    public static int compare(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5797
                              byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5798
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5799
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5800
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5801
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5802
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5803
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5804
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5805
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5806
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5807
            return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5808
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5809
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5810
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5811
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5812
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5813
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5814
     * Compares two {@code byte} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5815
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5816
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5817
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5818
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5819
     * {@link Byte#compareUnsigned(byte, byte)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5820
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5821
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5822
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5823
     * (See {@link #mismatch(byte[], byte[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5824
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5825
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5826
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5827
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5828
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5829
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5830
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5831
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5832
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5833
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5834
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5835
     *         return Byte.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5836
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5837
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5838
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5839
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5840
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5841
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5842
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5843
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5844
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5845
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5846
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5847
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5848
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5849
    public static int compareUnsigned(byte[] a, byte[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5850
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5851
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5852
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5853
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5854
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5855
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5856
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5857
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5858
            return Byte.compareUnsigned(a[i], b[i]);
33519
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
        return a.length - b.length;
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
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5865
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5866
     * Compares two {@code byte} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5867
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5868
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5869
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5870
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5871
     * elements, as if by {@link Byte#compareUnsigned(byte, byte)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5872
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5873
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5874
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5875
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5876
     * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5877
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5878
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5879
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5880
     * <p>This method behaves as if:
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
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5883
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5884
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5885
     *         return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5886
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5887
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5888
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5889
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5890
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5891
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5892
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5893
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5894
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5895
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5896
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5897
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5898
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5899
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5900
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5901
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5902
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5903
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5904
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5905
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5906
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5907
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5908
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5909
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5910
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5911
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5912
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5913
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5914
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5915
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5916
    public static int compareUnsigned(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5917
                                      byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5918
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5919
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5920
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5921
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5922
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5923
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5924
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5925
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5926
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5927
            return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5928
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5929
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5930
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5931
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5932
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5933
    // Compare short
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5934
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5935
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5936
     * Compares two {@code short} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5937
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5938
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5939
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5940
     * {@link Short#compare(short, short)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5941
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5942
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5943
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5944
     * (See {@link #mismatch(short[], short[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5945
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5946
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5947
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5948
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5949
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5950
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5951
     * <p>The comparison is consistent with {@link #equals(short[], short[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5952
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5953
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5954
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5955
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5956
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5957
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5958
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5959
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5960
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5961
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5962
     *         return Short.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5963
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5964
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5965
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5966
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5967
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5968
     * @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
  5969
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5970
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5971
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5972
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5973
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5974
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5975
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5976
    public static int compare(short[] a, short[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5977
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5978
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5979
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5980
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5981
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5982
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5983
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5984
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5985
            return Short.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5986
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5987
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5988
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5989
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5990
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5991
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5992
     * Compares two {@code short} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5993
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5994
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5995
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5996
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5997
     * elements, as if by {@link Short#compare(short, short)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5998
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5999
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6000
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6001
     * (See {@link #mismatch(short[], int, int, short[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6002
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6003
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6004
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6005
     * {@link #equals(short[], int, int, short[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6006
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6007
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6008
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6009
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6010
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6011
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6012
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6013
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6014
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6015
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6016
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6017
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6018
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6019
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6020
     *         return Short.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6021
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6022
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6023
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6024
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6025
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6026
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6027
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6028
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6029
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6030
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6031
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6032
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6033
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6034
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6035
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6036
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6037
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6038
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6039
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6040
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6041
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6042
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6043
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6044
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6045
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6046
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6047
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6048
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6049
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6050
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6051
    public static int compare(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6052
                              short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6053
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6054
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6055
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6056
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6057
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6058
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6059
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6060
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6061
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6062
            return Short.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6063
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6064
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6065
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6066
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6067
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6068
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6069
     * Compares two {@code short} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6070
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6071
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6072
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6073
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6074
     * {@link Short#compareUnsigned(short, short)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6075
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6076
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6077
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6078
     * (See {@link #mismatch(short[], short[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6079
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6080
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6081
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6082
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6083
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6084
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6085
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6086
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6087
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6088
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6089
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6090
     *         return Short.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6091
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6092
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6093
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6094
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6095
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6096
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6097
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6098
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6099
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6100
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6101
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6102
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6103
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6104
    public static int compareUnsigned(short[] a, short[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6105
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6106
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6107
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6108
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6109
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6110
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6111
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6112
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6113
            return Short.compareUnsigned(a[i], b[i]);
33519
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
        return a.length - b.length;
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
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6120
     * Compares two {@code short} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6121
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6122
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6123
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6124
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6125
     * elements, as if by {@link Short#compareUnsigned(short, short)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6126
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6127
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6128
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6129
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6130
     * (See {@link #mismatch(short[], int, int, short[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6131
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6132
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6133
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6134
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6135
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6136
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6137
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6138
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6139
     *         return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6140
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6141
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6142
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6143
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6144
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6145
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6146
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6147
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6148
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6149
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6150
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6151
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6152
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6153
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6154
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6155
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6156
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6157
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6158
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6159
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6160
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6161
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6162
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6163
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6164
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6165
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6166
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6167
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6168
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6169
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6170
    public static int compareUnsigned(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6171
                                      short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6172
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6173
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6174
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6175
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6176
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6177
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6178
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6179
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6180
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6181
            return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6182
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6183
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6184
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6185
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6186
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6187
    // Compare char
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6188
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6189
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6190
     * Compares two {@code char} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6191
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6192
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6193
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6194
     * {@link Character#compare(char, char)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6195
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6196
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6197
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6198
     * (See {@link #mismatch(char[], char[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6199
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6200
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6201
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6202
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6203
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6204
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6205
     * <p>The comparison is consistent with {@link #equals(char[], char[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6206
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6207
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6208
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6209
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6210
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6211
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6212
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6213
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6214
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6215
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6216
     *         return Character.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6217
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6218
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6219
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6220
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6221
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6222
     * @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
  6223
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6224
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6225
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6226
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6227
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6228
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6229
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6230
    public static int compare(char[] a, char[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6231
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6232
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6233
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6234
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6235
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6236
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6237
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6238
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6239
            return Character.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6240
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6241
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6242
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6243
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6244
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6245
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6246
     * Compares two {@code char} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6247
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6248
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6249
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6250
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6251
     * elements, as if by {@link Character#compare(char, char)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6252
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6253
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6254
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6255
     * (See {@link #mismatch(char[], int, int, char[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6256
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6257
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6258
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6259
     * {@link #equals(char[], int, int, char[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6260
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6261
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6262
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6263
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6264
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6265
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6266
     * }</pre>
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:
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, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6272
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6273
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6274
     *         return Character.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6275
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6276
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6277
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6278
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6279
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6280
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6281
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6282
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6283
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6284
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6285
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6286
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6287
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6288
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6289
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6290
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6291
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6292
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6293
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6294
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6295
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6296
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6297
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6298
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6299
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6300
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6301
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6302
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6303
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6304
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6305
    public static int compare(char[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6306
                              char[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6307
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6308
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6309
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6310
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6311
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6312
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6313
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6314
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6315
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6316
            return Character.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6317
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6318
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6319
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6320
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6321
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6322
    // Compare int
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6323
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6324
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6325
     * Compares two {@code int} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6326
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6327
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6328
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6329
     * {@link Integer#compare(int, int)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6330
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6331
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6332
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6333
     * (See {@link #mismatch(int[], int[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6334
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6335
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6336
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6337
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6338
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6339
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6340
     * <p>The comparison is consistent with {@link #equals(int[], int[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6341
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6342
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6343
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6344
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6345
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6346
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6347
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6348
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6349
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6350
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6351
     *         return Integer.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6352
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6353
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6354
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6355
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6356
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6357
     * @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
  6358
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6359
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6360
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6361
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6362
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6363
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6364
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6365
    public static int compare(int[] a, int[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6366
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6367
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6368
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6369
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6370
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6371
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6372
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6373
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6374
            return Integer.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6375
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6376
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6377
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6378
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6379
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6380
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6381
     * Compares two {@code int} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6382
     * ranges.
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>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6385
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6386
     * elements, as if by {@link Integer#compare(int, int)}, at a relative index
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6387
     * within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6388
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6389
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6390
     * (See {@link #mismatch(int[], int, int, int[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6391
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6392
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6393
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6394
     * {@link #equals(int[], int, int, int[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6395
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6396
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6397
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6398
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6399
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6400
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
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
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6404
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6405
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6406
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6407
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6408
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6409
     *         return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6410
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6411
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6412
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6413
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6414
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6415
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6416
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6417
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6418
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6419
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6420
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6421
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6422
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6423
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6424
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6425
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6426
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6427
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6428
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6429
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6430
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6431
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6432
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6433
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6434
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6435
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6436
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6437
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6438
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6439
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6440
    public static int compare(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6441
                              int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6442
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6443
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6444
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6445
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6446
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6447
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6448
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6449
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6450
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6451
            return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6452
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6453
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6454
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6455
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6456
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6457
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6458
     * Compares two {@code int} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6459
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6460
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6461
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6462
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6463
     * {@link Integer#compareUnsigned(int, int)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6464
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6465
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6466
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6467
     * (See {@link #mismatch(int[], int[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6468
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6469
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6470
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6471
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6472
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6473
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6474
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6475
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6476
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6477
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6478
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6479
     *         return Integer.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6480
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6481
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6482
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6483
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6484
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6485
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6486
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6487
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6488
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6489
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6490
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6491
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6492
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6493
    public static int compareUnsigned(int[] a, int[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6494
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6495
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6496
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6497
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6498
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6499
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6500
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6501
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6502
            return Integer.compareUnsigned(a[i], b[i]);
33519
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
        return a.length - b.length;
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
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6509
     * Compares two {@code int} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6510
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6511
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6512
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6513
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6514
     * elements, as if by {@link Integer#compareUnsigned(int, int)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6515
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6516
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6517
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6518
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6519
     * (See {@link #mismatch(int[], int, int, int[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6520
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6521
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6522
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6523
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6524
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6525
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6526
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6527
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6528
     *         return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6529
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6530
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6531
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6532
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6533
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6534
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6535
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6536
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6537
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6538
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6539
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6540
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6541
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6542
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6543
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6544
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6545
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6546
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6547
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6548
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6549
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6550
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6551
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6552
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6553
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6554
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6555
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6556
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6557
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6558
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6559
    public static int compareUnsigned(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6560
                                      int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6561
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6562
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6563
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6564
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6565
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6566
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6567
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6568
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6569
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6570
            return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6571
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6572
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6573
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6574
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6575
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6576
    // Compare long
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6577
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6578
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6579
     * Compares two {@code long} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6580
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6581
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6582
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6583
     * {@link Long#compare(long, long)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6584
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6585
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6586
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6587
     * (See {@link #mismatch(long[], long[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6588
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6589
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6590
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6591
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6592
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6593
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6594
     * <p>The comparison is consistent with {@link #equals(long[], long[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6595
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6596
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6597
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6598
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6599
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6600
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6601
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6602
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6603
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6604
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6605
     *         return Long.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6606
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6607
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6608
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6609
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6610
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6611
     * @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
  6612
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6613
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6614
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6615
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6616
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6617
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6618
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6619
    public static int compare(long[] a, long[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6620
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6621
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6622
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6623
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6624
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6625
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6626
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6627
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6628
            return Long.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6629
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6630
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6631
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6632
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6633
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6634
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6635
     * Compares two {@code long} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6636
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6637
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6638
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6639
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6640
     * elements, as if by {@link Long#compare(long, long)}, at a relative index
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6641
     * within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6642
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6643
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6644
     * (See {@link #mismatch(long[], int, int, long[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6645
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6646
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6647
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6648
     * {@link #equals(long[], int, int, long[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6649
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6650
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6651
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6652
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6653
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6654
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6655
     * }</pre>
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:
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, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6661
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6662
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6663
     *         return Long.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6664
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6665
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6666
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6667
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6668
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6669
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6670
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6671
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6672
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6673
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6674
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6675
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6676
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6677
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6678
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6679
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6680
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6681
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6682
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6683
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6684
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6685
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6686
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6687
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6688
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6689
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6690
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6691
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6692
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6693
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6694
    public static int compare(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6695
                              long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6696
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6697
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6698
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6699
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6700
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6701
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6702
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6703
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6704
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6705
            return Long.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6706
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6707
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6708
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6709
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6710
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6711
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6712
     * Compares two {@code long} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6713
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6714
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6715
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6716
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6717
     * {@link Long#compareUnsigned(long, long)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6718
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6719
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6720
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6721
     * (See {@link #mismatch(long[], long[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6722
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6723
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6724
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6725
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6726
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6727
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6728
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6729
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6730
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6731
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6732
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6733
     *         return Long.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6734
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6735
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6736
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6737
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6738
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6739
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6740
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6741
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6742
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6743
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6744
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6745
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6746
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6747
    public static int compareUnsigned(long[] a, long[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6748
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6749
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6750
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6751
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6752
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6753
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6754
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6755
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6756
            return Long.compareUnsigned(a[i], b[i]);
33519
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
        return a.length - b.length;
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
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6763
     * Compares two {@code long} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6764
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6765
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6766
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6767
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6768
     * elements, as if by {@link Long#compareUnsigned(long, long)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6769
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6770
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6771
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6772
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6773
     * (See {@link #mismatch(long[], int, int, long[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6774
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6775
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6776
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6777
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6778
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6779
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6780
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6781
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6782
     *         return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6783
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6784
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6785
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6786
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6787
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6788
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6789
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6790
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6791
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6792
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6793
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6794
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6795
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6796
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6797
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6798
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6799
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6800
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6801
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6802
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6803
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6804
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6805
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6806
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6807
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6808
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6809
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6810
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6811
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6812
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6813
    public static int compareUnsigned(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6814
                                      long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6815
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6816
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6817
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6818
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6819
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6820
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6821
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6822
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6823
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6824
            return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6825
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6826
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6827
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6828
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6829
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6830
    // Compare float
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6831
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6832
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6833
     * Compares two {@code float} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6834
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6835
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6836
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6837
     * {@link Float#compare(float, float)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6838
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6839
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6840
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6841
     * (See {@link #mismatch(float[], float[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6842
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6843
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6844
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6845
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6846
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6847
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6848
     * <p>The comparison is consistent with {@link #equals(float[], float[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6849
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6850
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6851
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6852
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6853
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6854
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6855
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6856
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6857
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6858
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6859
     *         return Float.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6860
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6861
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6862
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6863
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6864
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6865
     * @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
  6866
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6867
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6868
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6869
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6870
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6871
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6872
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6873
    public static int compare(float[] a, float[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6874
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6875
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6876
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6877
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6878
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6879
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6880
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6881
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6882
            return Float.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6883
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6884
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6885
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6886
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6887
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6888
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6889
     * Compares two {@code float} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6890
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6891
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6892
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6893
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6894
     * elements, as if by {@link Float#compare(float, float)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6895
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6896
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6897
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6898
     * (See {@link #mismatch(float[], int, int, float[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6899
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6900
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6901
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6902
     * {@link #equals(float[], int, int, float[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6903
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6904
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6905
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6906
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6907
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6908
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6909
     * }</pre>
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:
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, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6915
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6916
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6917
     *         return Float.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6918
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6919
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6920
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6921
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6922
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6923
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6924
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6925
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6926
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6927
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6928
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6929
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6930
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6931
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6932
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6933
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6934
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6935
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6936
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6937
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6938
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6939
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6940
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6941
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6942
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6943
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6944
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6945
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6946
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6947
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6948
    public static int compare(float[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6949
                              float[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6950
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6951
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6952
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6953
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6954
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6955
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6956
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6957
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6958
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6959
            return Float.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6960
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6961
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6962
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6963
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6964
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6965
    // Compare double
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6966
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6967
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6968
     * Compares two {@code double} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6969
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6970
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6971
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6972
     * {@link Double#compare(double, double)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6973
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6974
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6975
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6976
     * (See {@link #mismatch(double[], double[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6977
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6978
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6979
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6980
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6981
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6982
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6983
     * <p>The comparison is consistent with {@link #equals(double[], double[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6984
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6985
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6986
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6987
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6988
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6989
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6990
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6991
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6992
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6993
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6994
     *         return Double.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6995
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6996
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6997
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6998
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6999
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7000
     * @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
  7001
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7002
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7003
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7004
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7005
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7006
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7007
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7008
    public static int compare(double[] a, double[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7009
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7010
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7011
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7012
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7013
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7014
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7015
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7016
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7017
            return Double.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7018
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7019
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7020
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7021
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7022
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7023
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7024
     * Compares two {@code double} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7025
     * ranges.
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>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7028
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7029
     * elements, as if by {@link Double#compare(double, double)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7030
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7031
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7032
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7033
     * (See {@link #mismatch(double[], int, int, double[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7034
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7035
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7036
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7037
     * {@link #equals(double[], int, int, double[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7038
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7039
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7040
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7041
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7042
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7043
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
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
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7047
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7048
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7049
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7050
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7051
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7052
     *         return Double.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7053
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7054
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7055
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7056
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7057
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7058
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7059
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7060
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7061
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7062
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7063
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7064
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7065
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7066
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7067
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7068
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7069
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7070
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7071
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7072
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7073
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7074
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7075
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7076
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7077
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7078
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7079
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7080
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7081
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7082
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7083
    public static int compare(double[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7084
                              double[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7085
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7086
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7087
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7088
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7089
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7090
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7091
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7092
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7093
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7094
            return Double.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7095
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7096
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7097
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7098
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7099
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7100
    // Compare objects
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7101
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7102
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7103
     * Compares two {@code Object} arrays, within comparable elements,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7104
     * lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7105
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7106
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7107
     * comparison is the result of comparing two elements of type {@code T} at
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7108
     * an index {@code i} within the respective arrays that is the prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7109
     * length, as if by:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7110
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7111
     *     Comparator.nullsFirst(Comparator.<T>naturalOrder()).
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7112
     *         compare(a[i], b[i])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7113
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7114
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7115
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7116
     * (See {@link #mismatch(Object[], Object[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7117
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7118
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7119
     * <p>A {@code null} array reference is considered lexicographically less
59298
de2896bc018a 8234799: javadoc of java.util.Arrays.compare(T[] a, T[] b) misses the word "less"
jboes
parents: 59055
diff changeset
  7120
     * than a non-{@code null} array reference. Two {@code null} array
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7121
     * references are considered equal.
59298
de2896bc018a 8234799: javadoc of java.util.Arrays.compare(T[] a, T[] b) misses the word "less"
jboes
parents: 59055
diff changeset
  7122
     * A {@code null} array element is considered lexicographically less than a
de2896bc018a 8234799: javadoc of java.util.Arrays.compare(T[] a, T[] b) misses the word "less"
jboes
parents: 59055
diff changeset
  7123
     * non-{@code null} array element. Two {@code null} array elements are
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7124
     * considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7125
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7126
     * <p>The comparison is consistent with {@link #equals(Object[], Object[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7127
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7128
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7129
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7130
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7131
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7132
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7133
     * <p>This method behaves as if (for non-{@code null} array references
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7134
     * and elements):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7135
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7136
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7137
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7138
     *         return a[i].compareTo(b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7139
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7140
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7141
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7142
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7143
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7144
     * @param <T> the type of comparable array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7145
     * @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
  7146
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7147
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7148
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7149
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7150
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7151
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7152
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7153
    public static <T extends Comparable<? super T>> int compare(T[] a, T[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7154
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7155
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7156
        // A null array is less than a non-null array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7157
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7158
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7159
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7160
        int length = Math.min(a.length, b.length);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7161
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7162
            T oa = a[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7163
            T ob = b[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7164
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7165
                // A null element is less than a non-null element
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7166
                if (oa == null || ob == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7167
                    return oa == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7168
                int v = oa.compareTo(ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7169
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7170
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7171
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7172
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7173
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7174
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7175
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7176
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7177
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7178
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7179
     * Compares two {@code Object} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7180
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7181
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7182
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7183
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7184
     * elements of type {@code T} at a relative index {@code i} within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7185
     * respective arrays that is the prefix length, as if by:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7186
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7187
     *     Comparator.nullsFirst(Comparator.<T>naturalOrder()).
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7188
     *         compare(a[aFromIndex + i, b[bFromIndex + i])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7189
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7190
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7191
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7192
     * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7193
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7194
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7195
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7196
     * {@link #equals(Object[], int, int, Object[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7197
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7198
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7199
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7200
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7201
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7202
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7203
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7204
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7205
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7206
     * <p>This method behaves as if (for non-{@code null} array elements):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7207
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7208
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7209
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7210
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7211
     *         return a[aFromIndex + i].compareTo(b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7212
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7213
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7214
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7215
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7216
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7217
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7218
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7219
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7220
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7221
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7222
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7223
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7224
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7225
     * @param <T> the type of comparable array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7226
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7227
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7228
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7229
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7230
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7231
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7232
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7233
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7234
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7235
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7236
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7237
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7238
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7239
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7240
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7241
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7242
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7243
    public static <T extends Comparable<? super T>> int compare(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7244
            T[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7245
            T[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7246
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7247
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7248
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7249
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7250
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7251
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7252
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7253
            T oa = a[aFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7254
            T ob = b[bFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7255
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7256
                if (oa == null || ob == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7257
                    return oa == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7258
                int v = oa.compareTo(ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7259
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7260
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7261
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7262
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7263
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7264
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7265
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7266
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7267
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7268
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7269
     * Compares two {@code Object} arrays lexicographically using a specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7270
     * comparator.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7271
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7272
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7273
     * comparison is the result of comparing with the specified comparator two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7274
     * elements at an index within the respective arrays that is the prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7275
     * length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7276
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7277
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7278
     * (See {@link #mismatch(Object[], Object[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7279
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7280
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7281
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7282
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7283
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7284
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7285
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7286
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7287
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7288
     *     int i = Arrays.mismatch(a, b, cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7289
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7290
     *         return cmp.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7291
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7292
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7293
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7294
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7295
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7296
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7297
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7298
     * @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
  7299
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7300
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7301
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7302
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7303
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7304
     * @throws NullPointerException if the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7305
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7306
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7307
    public static <T> int compare(T[] a, T[] b,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7308
                                  Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7309
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7310
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7311
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7312
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7313
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7314
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7315
        int length = Math.min(a.length, b.length);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7316
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7317
            T oa = a[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7318
            T ob = b[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7319
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7320
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7321
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7322
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7323
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7324
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7325
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7326
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7327
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7328
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7329
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7330
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7331
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7332
     * Compares two {@code Object} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7333
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7334
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7335
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7336
     * then the lexicographic comparison is the result of comparing with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7337
     * specified comparator two elements at a relative index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7338
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7339
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7340
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7341
     * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7342
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7343
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7344
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7345
     * <p>This method behaves as if (for non-{@code null} array elements):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7346
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7347
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7348
     *                             b, bFromIndex, bToIndex, cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7349
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7350
     *         return cmp.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7351
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7352
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7353
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7354
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7355
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7356
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7357
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7358
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7359
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7360
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7361
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7362
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7363
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7364
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7365
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7366
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7367
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7368
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7369
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7370
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7371
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7372
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7373
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7374
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7375
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7376
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7377
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7378
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7379
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7380
     *         if either array or the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7381
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7382
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7383
    public static <T> int compare(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7384
            T[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7385
            T[] b, int bFromIndex, int bToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7386
            Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7387
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7388
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7389
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7390
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7391
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7392
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7393
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7394
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7395
            T oa = a[aFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7396
            T ob = b[bFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7397
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7398
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7399
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7400
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7401
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7402
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7403
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7404
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7405
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7406
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7407
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7408
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7409
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7410
    // Mismatch methods
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7411
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7412
    // Mismatch boolean
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7413
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7414
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7415
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7416
     * {@code boolean} arrays, otherwise return -1 if no mismatch is found.  The
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7417
     * 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
  7418
     * of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7419
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7420
     * <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
  7421
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7422
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7423
     * 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
  7424
     * 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
  7425
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7426
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7427
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7428
     * <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
  7429
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7430
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7431
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7432
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7433
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7434
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7435
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7436
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7437
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7438
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7439
     * <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
  7440
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7441
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7442
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7443
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7444
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7445
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7446
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7447
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7448
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7449
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7450
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7451
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7452
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7453
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7454
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7455
    public static int mismatch(boolean[] a, boolean[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7456
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7457
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7458
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7459
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7460
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7461
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7462
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7463
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7464
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7465
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7466
     * {@code boolean} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7467
     * 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
  7468
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7469
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7470
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7471
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7472
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7473
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7474
     * 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
  7475
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7476
     * 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
  7477
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7478
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7479
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7480
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7481
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7482
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7483
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7484
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7485
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7486
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7487
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7488
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7489
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7490
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7491
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7492
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7493
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7494
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7495
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7496
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7497
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7498
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7499
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7500
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7501
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7502
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7503
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7504
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7505
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7506
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7507
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7508
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7509
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7510
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7511
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7512
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7513
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7514
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7515
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7516
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7517
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7518
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7519
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7520
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7521
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7522
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7523
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7524
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7525
    public static int mismatch(boolean[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7526
                               boolean[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7527
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7528
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7529
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7530
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7531
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7532
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7533
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7534
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7535
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7536
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7537
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7538
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7539
    // Mismatch byte
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7540
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7541
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7542
     * Finds and returns the index of the first mismatch between two {@code byte}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7543
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7544
     * 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
  7545
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7546
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7547
     * <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
  7548
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7549
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7550
     * 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
  7551
     * 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
  7552
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7553
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7554
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7555
     * <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
  7556
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7557
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7558
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7559
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7560
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7561
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7562
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7563
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7564
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7565
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7566
     * <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
  7567
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7568
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7569
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7570
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7571
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7572
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7573
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7574
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7575
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7576
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7577
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7578
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7579
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7580
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7581
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7582
    public static int mismatch(byte[] a, byte[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7583
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7584
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7585
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7586
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7587
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7588
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7589
    }
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
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7593
     * {@code byte} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7594
     * 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
  7595
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7596
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7597
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7598
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7599
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7600
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7601
     * 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
  7602
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7603
     * 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
  7604
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7605
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7606
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7607
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7608
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7609
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7610
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7611
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7612
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7613
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7614
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7615
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7616
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7617
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7618
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7619
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7620
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7621
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7622
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7623
     * 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
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7626
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7627
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
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 aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7632
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7633
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7634
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7635
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7636
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7637
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7638
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7639
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7640
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7641
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7642
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7643
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7644
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7645
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7646
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7647
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7648
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7649
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7650
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7651
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7652
    public static int mismatch(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7653
                               byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7654
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7655
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7656
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7657
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7658
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7659
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7660
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7661
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7662
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7663
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7664
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7665
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7666
    // Mismatch char
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7667
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7668
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7669
     * Finds and returns the index of the first mismatch between two {@code char}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7670
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7671
     * 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
  7672
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7673
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7674
     * <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
  7675
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7676
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7677
     * 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
  7678
     * 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
  7679
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7680
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7681
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7682
     * <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
  7683
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7684
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7685
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7686
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7687
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7688
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7689
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7690
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7691
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7692
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7693
     * <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
  7694
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7695
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7696
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7697
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7698
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7699
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7700
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7701
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7702
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7703
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7704
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7705
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7706
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7707
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7708
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7709
    public static int mismatch(char[] a, char[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7710
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7711
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7712
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7713
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7714
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7715
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7716
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7717
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7718
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7719
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7720
     * {@code char} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7721
     * 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
  7722
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7723
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7724
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7725
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7726
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7727
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7728
     * 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
  7729
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7730
     * 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
  7731
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7732
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7733
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7734
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7735
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7736
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7737
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7738
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7739
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7740
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7741
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7742
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7743
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7744
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7745
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7746
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7747
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7748
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7749
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7750
     * 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
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7753
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7754
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
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 aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7759
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7760
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7761
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7762
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7763
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7764
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7765
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7766
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7767
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7768
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7769
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7770
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7771
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7772
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7773
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7774
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7775
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7776
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7777
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7778
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7779
    public static int mismatch(char[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7780
                               char[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7781
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7782
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7783
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7784
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7785
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7786
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7787
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7788
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7789
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7790
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7791
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7792
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7793
    // Mismatch short
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7794
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7795
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7796
     * Finds and returns the index of the first mismatch between two {@code short}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7797
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7798
     * 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
  7799
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7800
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7801
     * <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
  7802
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7803
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7804
     * 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
  7805
     * 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
  7806
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7807
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7808
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7809
     * <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
  7810
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7811
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7812
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7813
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7814
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7815
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7816
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7817
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7818
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7819
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7820
     * <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
  7821
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7822
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7823
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7824
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7825
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7826
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7827
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7828
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7829
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7830
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7831
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7832
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7833
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7834
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7835
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7836
    public static int mismatch(short[] a, short[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7837
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7838
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7839
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7840
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7841
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7842
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7843
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7844
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7845
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7846
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7847
     * {@code short} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7848
     * 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
  7849
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7850
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7851
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7852
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7853
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7854
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7855
     * 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
  7856
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7857
     * 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
  7858
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7859
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7860
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7861
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7862
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7863
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7864
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7865
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7866
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7867
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7868
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7869
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7870
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7871
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7872
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7873
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7874
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7875
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7876
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7877
     * 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
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7880
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7881
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
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 aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7886
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7887
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7888
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7889
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7890
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7891
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7892
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7893
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7894
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7895
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7896
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7897
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7898
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7899
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7900
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7901
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7902
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7903
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7904
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7905
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7906
    public static int mismatch(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7907
                               short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7908
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7909
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7910
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7911
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7912
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7913
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7914
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7915
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7916
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7917
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7918
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7919
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7920
    // Mismatch int
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7921
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7922
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7923
     * Finds and returns the index of the first mismatch between two {@code int}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7924
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7925
     * 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
  7926
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7927
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7928
     * <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
  7929
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7930
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7931
     * 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
  7932
     * 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
  7933
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7934
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7935
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7936
     * <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
  7937
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7938
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7939
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7940
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7941
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7942
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7943
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7944
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7945
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7946
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7947
     * <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
  7948
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7949
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7950
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7951
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7952
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7953
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7954
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7955
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7956
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7957
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7958
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7959
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7960
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7961
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7962
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7963
    public static int mismatch(int[] a, int[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7964
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7965
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7966
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7967
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7968
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7969
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7970
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7971
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7972
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7973
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7974
     * {@code int} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7975
     * 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
  7976
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7977
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7978
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7979
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7980
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7981
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7982
     * 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
  7983
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7984
     * 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
  7985
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7986
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7987
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7988
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7989
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7990
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7991
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7992
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7993
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7994
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7995
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7996
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7997
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7998
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7999
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8000
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8001
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8002
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8003
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8004
     * 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
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8007
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8008
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
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 aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8013
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8014
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8015
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8016
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8017
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8018
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8019
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8020
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8021
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8022
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8023
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8024
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8025
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8026
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8027
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8028
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8029
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8030
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8031
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8032
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8033
    public static int mismatch(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8034
                               int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8035
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8036
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8037
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8038
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8039
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8040
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8041
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8042
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8043
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8044
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8045
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8046
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8047
    // Mismatch long
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8048
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8049
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8050
     * Finds and returns the index of the first mismatch between two {@code long}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8051
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8052
     * 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
  8053
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8054
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8055
     * <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
  8056
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8057
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8058
     * 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
  8059
     * 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
  8060
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8061
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8062
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8063
     * <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
  8064
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8065
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8066
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8067
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8068
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8069
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8070
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8071
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8072
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8073
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8074
     * <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
  8075
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8076
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8077
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8078
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8079
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8080
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8081
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8082
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8083
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8084
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8085
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8086
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8087
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8088
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8089
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8090
    public static int mismatch(long[] a, long[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8091
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8092
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8093
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8094
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8095
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8096
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8097
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8098
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8099
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8100
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8101
     * {@code long} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8102
     * 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
  8103
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8104
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8105
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8106
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8107
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8108
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8109
     * 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
  8110
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8111
     * 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
  8112
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8113
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8114
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8115
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8116
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8117
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8118
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8119
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8120
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8121
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8122
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8123
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8124
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8125
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8126
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8127
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8128
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8129
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8130
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8131
     * 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
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8134
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8135
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
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 aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8140
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8141
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8142
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8143
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8144
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8145
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8146
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8147
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8148
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8149
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8150
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8151
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8152
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8153
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8154
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8155
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8156
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8157
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8158
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8159
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8160
    public static int mismatch(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8161
                               long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8162
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8163
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8164
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8165
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8166
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8167
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8168
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8169
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8170
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8171
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8172
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8173
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8174
    // Mismatch float
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8175
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8176
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8177
     * Finds and returns the index of the first mismatch between two {@code float}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8178
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8179
     * 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
  8180
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8181
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8182
     * <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
  8183
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8184
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8185
     * 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
  8186
     * 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
  8187
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8188
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8189
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8190
     * <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
  8191
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8192
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8193
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8194
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8195
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8196
     *     Float.compare(a[pl], b[pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8197
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8198
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8199
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8200
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8201
     * <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
  8202
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8203
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8204
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8205
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8206
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8207
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8208
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8209
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8210
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8211
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8212
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8213
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8214
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8215
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8216
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8217
    public static int mismatch(float[] a, float[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8218
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8219
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8220
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8221
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8222
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8223
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8224
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8225
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8226
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8227
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8228
     * {@code float} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8229
     * 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
  8230
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8231
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8232
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8233
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8234
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8235
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8236
     * 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
  8237
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8238
     * 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
  8239
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8240
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8241
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8242
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8243
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8244
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8245
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8246
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8247
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8248
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8249
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8250
     *     Float.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8251
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8252
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8253
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8254
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8255
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8256
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8257
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8258
     * 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
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8261
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8262
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
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 aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8267
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8268
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8269
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8270
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8271
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8272
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8273
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8274
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8275
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8276
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8277
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8278
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8279
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8280
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8281
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8282
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8283
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8284
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8285
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8286
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8287
    public static int mismatch(float[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8288
                               float[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8289
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8290
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8291
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8292
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8293
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8294
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8295
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8296
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8297
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8298
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8299
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8300
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8301
    // Mismatch double
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8302
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8303
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8304
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8305
     * {@code double} arrays, otherwise return -1 if no mismatch is found.  The
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8306
     * 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
  8307
     * of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8308
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8309
     * <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
  8310
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8311
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8312
     * 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
  8313
     * 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
  8314
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8315
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8316
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8317
     * <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
  8318
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8319
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8320
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8321
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8322
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8323
     *     Double.compare(a[pl], b[pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8324
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8325
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8326
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8327
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8328
     * <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
  8329
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8330
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8331
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8332
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8333
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8334
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8335
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8336
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8337
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8338
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8339
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8340
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8341
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8342
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8343
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8344
    public static int mismatch(double[] a, double[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8345
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8346
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8347
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8348
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8349
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8350
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8351
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8352
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8353
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8354
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8355
     * {@code double} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8356
     * 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
  8357
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8358
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8359
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8360
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8361
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8362
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8363
     * 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
  8364
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8365
     * 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
  8366
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8367
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8368
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8369
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8370
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8371
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8372
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8373
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8374
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8375
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8376
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8377
     *     Double.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8378
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8379
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8380
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8381
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8382
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8383
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8384
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8385
     * 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
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8388
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8389
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
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 aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8394
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8395
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8396
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8397
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8398
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8399
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8400
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8401
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8402
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8403
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8404
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8405
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8406
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8407
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8408
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8409
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8410
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8411
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8412
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8413
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8414
    public static int mismatch(double[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8415
                               double[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8416
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8417
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8418
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8419
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8420
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8421
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8422
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8423
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8424
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8425
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8426
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8427
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8428
    // Mismatch objects
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8429
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8430
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8431
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8432
     * {@code Object} arrays, otherwise return -1 if no mismatch is found.  The
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8433
     * 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
  8434
     * of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8435
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8436
     * <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
  8437
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8438
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8439
     * 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
  8440
     * 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
  8441
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8442
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8443
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8444
     * <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
  8445
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8446
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8447
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8448
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8449
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8450
     *     !Objects.equals(a[pl], b[pl])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8451
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8452
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8453
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8454
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8455
     * <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
  8456
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8457
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8458
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8459
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8460
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8461
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8462
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8463
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8464
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8465
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8466
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8467
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8468
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8469
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8470
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8471
    public static int mismatch(Object[] a, Object[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8472
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8473
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8474
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8475
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8476
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8477
            if (!Objects.equals(a[i], b[i]))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8478
                return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8479
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8480
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8481
        return a.length != b.length ? length : -1;
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
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8485
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8486
     * {@code Object} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8487
     * 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
  8488
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8489
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8490
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8491
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8492
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8493
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8494
     * 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
  8495
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8496
     * 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
  8497
     * larger range.
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} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8501
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8502
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8503
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8504
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8505
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8506
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8507
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8508
     *     !Objects.equals(a[aFromIndex + pl], b[bFromIndex + pl])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8509
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8510
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8511
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8512
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8513
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8514
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8515
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8516
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8517
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8518
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8519
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8520
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8521
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8522
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8523
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8524
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8525
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8526
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8527
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8528
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8529
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8530
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8531
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8532
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8533
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8534
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8535
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8536
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8537
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8538
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8539
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8540
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8541
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8542
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8543
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8544
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8545
    public static int mismatch(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8546
            Object[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8547
            Object[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8548
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8549
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8550
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8551
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8552
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8553
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8554
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8555
            if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8556
                return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8557
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8558
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8559
        return aLength != bLength ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8560
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8561
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8562
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8563
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8564
     * {@code Object} arrays, otherwise return -1 if no mismatch is found.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8565
     * 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
  8566
     * (inclusive) of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8567
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8568
     * <p>The specified comparator is used to determine if two array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8569
     * from the each array are not equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8570
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8571
     * <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
  8572
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8573
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8574
     * 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
  8575
     * 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
  8576
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8577
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8578
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8579
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8580
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8581
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8582
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8583
     *     pl < Math.min(a.length, b.length) &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8584
     *     Arrays.equals(a, 0, pl, b, 0, pl, cmp)
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8585
     *     cmp.compare(a[pl], b[pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8586
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8587
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8588
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8589
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8590
     * <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
  8591
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8592
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8593
     *     a.length != b.length &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8594
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8595
     *                   b, 0, Math.min(a.length, b.length),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8596
     *                   cmp)
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8597
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8598
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8599
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8600
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8601
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8602
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8603
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8604
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8605
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8606
     *         if either array or the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8607
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8608
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8609
    public static <T> int mismatch(T[] a, T[] b, Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8610
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8611
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8612
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8613
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8614
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8615
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8616
            T oa = a[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8617
            T ob = b[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8618
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8619
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8620
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8621
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8622
                    return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8623
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8624
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8625
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8626
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8627
        return a.length != b.length ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8628
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8629
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8630
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8631
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8632
     * {@code Object} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8633
     * 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
  8634
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8635
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8636
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8637
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8638
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8639
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8640
     * 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
  8641
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8642
     * 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
  8643
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8644
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8645
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8646
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8647
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8648
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8649
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8650
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8651
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8652
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8653
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl, cmp) &&
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8654
     *     cmp.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8655
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8656
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8657
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8658
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8659
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8660
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8661
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8662
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8663
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8664
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8665
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8666
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8667
     *                   cmp)
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8668
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8669
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8670
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8671
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8672
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8673
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8674
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8675
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8676
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8677
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8678
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8679
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8680
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8681
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8682
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8683
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8684
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8685
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8686
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8687
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8688
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8689
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8690
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8691
     *         if either array or the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8692
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8693
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8694
    public static <T> int mismatch(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8695
            T[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8696
            T[] b, int bFromIndex, int bToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8697
            Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8698
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8699
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8700
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8701
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8702
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8703
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8704
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8705
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8706
            T oa = a[aFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8707
            T ob = b[bFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8708
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8709
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8710
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8711
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8712
                    return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8713
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8714
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8715
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8716
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8717
        return aLength != bLength ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8718
    }
44743
f0bbd698c486 8177789: fix collections framework links to point to java.util package doc
smarks
parents: 37685
diff changeset
  8719
}