Arrays.java revision 669c2cc344614fd721cfff2e7a9e80ad6e8b368c
151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/*
251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it
651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as
751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation.  Oracle designates this
851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided
951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code.
1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT
1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that
1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code).
1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version
1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation,
1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any
2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions.
2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.util;
2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.*;
2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/**
3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This class contains various methods for manipulating arrays (such as
3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sorting and searching). This class also contains a static factory
3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that allows arrays to be viewed as lists.
3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The methods in this class all throw a {@code NullPointerException},
3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the specified array reference is null, except where noted.
3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The documentation for the methods contained in this class includes
3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * briefs description of the <i>implementations</i>. Such descriptions should
4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be regarded as <i>implementation notes</i>, rather than parts of the
4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>specification</i>. Implementors should feel free to substitute other
4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * algorithms, so long as the specification itself is adhered to. (For
4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * example, the algorithm used by {@code sort(Object[])} does not have to be
4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a MergeSort, but it does have to be <i>stable</i>.)
4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This class is a member of the
4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <a href="{@docRoot}/../technotes/guides/collections/index.html">
4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Java Collections Framework</a>.
4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Josh Bloch
5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Neal Gafter
5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author John Rose
5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since  1.2
5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic class Arrays {
5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Suppresses default constructor, ensuring non-instantiability.
5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private Arrays() {}
5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /*
6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting of primitive type arrays.
6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(int[] a) {
7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a);
7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(int[] a, int fromIndex, int toIndex) {
10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(long[] a) {
11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a);
11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
13651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
13751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
13951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(long[] a, int fromIndex, int toIndex) {
14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
14351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
14651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
14851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
14951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
15051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(short[] a) {
15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a);
15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
16451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
17251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
17351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
17451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
17651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
17751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
17851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
17951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(short[] a, int fromIndex, int toIndex) {
18051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
18151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
18251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
18351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
18451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
18551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
18651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
18751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
18851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
18951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
19051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
19151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
19451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(char[] a) {
19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a);
19751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
19851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
19951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
20051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
20151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(char[] a, int fromIndex, int toIndex) {
22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
22351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
22651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(byte[] a) {
23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a);
23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
23851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
24151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
24451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
24551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
24651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
24751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
24851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
24951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
25051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(byte[] a, int fromIndex, int toIndex) {
26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
26151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
26251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
26651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
26751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The {@code <} relation does not provide a total order on all float
26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * value compares neither less than, greater than, nor equal to any value,
27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * even itself. This method uses the total order imposed by the method
27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * other value and all {@code Float.NaN} values are considered equal.
27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(float[] a) {
28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a);
28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The {@code <} relation does not provide a total order on all float
29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * value compares neither less than, greater than, nor equal to any value,
29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * even itself. This method uses the total order imposed by the method
29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * other value and all {@code Float.NaN} values are considered equal.
30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(float[] a, int fromIndex, int toIndex) {
31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
31951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
32151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
32251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
32351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The {@code <} relation does not provide a total order on all double
32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * value compares neither less than, greater than, nor equal to any value,
32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * even itself. This method uses the total order imposed by the method
32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * other value and all {@code Double.NaN} values are considered equal.
33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(double[] a) {
34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a);
34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
34251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The {@code <} relation does not provide a total order on all double
35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * value compares neither less than, greater than, nor equal to any value,
35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * even itself. This method uses the total order imposed by the method
35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * other value and all {@code Double.NaN} values are considered equal.
35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(double[] a, int fromIndex, int toIndex) {
37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /*
37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting of complex type arrays.
37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Old merge sort implementation can be selected (for
38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * compatibility with broken comparators) using a system property.
38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Cannot be a static boolean in the enclosing class due to
38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * circular dependencies. To be removed in a future release.
38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    static final class LegacyMergeSort {
3871ae149ae8db46adac5723c739d53dd82a1cf2e1aPiotr Jastrzebski        // Android-changed: Never use circular merge sort.
3881ae149ae8db46adac5723c739d53dd82a1cf2e1aPiotr Jastrzebski        private static final boolean userRequested = false;
38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /*
39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If this platform has an optimizing VM, check whether ComparableTimSort
39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers any performance benefit over TimSort in conjunction with a
39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * comparator that returns:
39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    {@code ((Comparable)first).compareTo(Second)}.
39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If not, you are better off deleting ComparableTimSort to
39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * eliminate the code duplication.  In other words, the commented
39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * out code below is the preferable implementation for sorting
39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays of Comparables if it offers sufficient performance.
40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    /**
40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//     * A comparator that implements the natural ordering of a group of
40451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//     * mutually comparable elements.  Using this comparator saves us
40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//     * from duplicating most of the code in this file (one version for
40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//     * Comparables, one for explicit Comparators).
40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//     */
40851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    private static final Comparator<Object> NATURAL_ORDER =
40951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//            new Comparator<Object>() {
41051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//        @SuppressWarnings("unchecked")
41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//        public int compare(Object first, Object second) {
41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//            return ((Comparable<Object>)first).compareTo(second);
41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//        }
41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    };
41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//
41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    public static void sort(Object[] a) {
41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//        sort(a, 0, a.length, NATURAL_ORDER);
41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    }
41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//
42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    public static void sort(Object[] a, int fromIndex, int toIndex) {
42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//        sort(a, fromIndex, toIndex, NATURAL_ORDER);
42251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    }
42351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
42551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array of objects into ascending order, according
42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to the {@linkplain Comparable natural ordering} of its elements.
42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * All elements in the array must implement the {@link Comparable}
42851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interface.  Furthermore, all elements in the array must be
42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not throw a {@code ClassCastException} for any elements {@code e1}
43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and {@code e2} in the array).
43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not be reordered as a result of the sort.
43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: This implementation is a stable, adaptive,
43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * iterative mergesort that requires far fewer than n lg(n) comparisons
43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * when the input array is partially sorted, while offering the
43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * performance of a traditional mergesort when the input array is
44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * randomly ordered.  If the input array is nearly sorted, the
44151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation requires approximately n comparisons.  Temporary
44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * storage requirements vary from a small constant for nearly sorted
44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input arrays to n/2 object references for randomly ordered input
44451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
44551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
44651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation takes equal advantage of ascending and
44751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * descending order in its input array, and can take advantage of
44851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending and descending order in different parts of the the same
44951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input array.  It is well-suited to merging two or more sorted arrays:
45051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * simply concatenate the arrays and sort the resulting array.
45151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
45251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation was adapted from Tim Peters's list sort for Python
45351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
45451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
45551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting and Information Theoretic Complexity", in Proceedings of the
45651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
45751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * January 1993.
45851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
45951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are not
46151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>mutually comparable</i> (for example, strings and integers)
46251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException (optional) if the natural
46351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         ordering of the array elements is found to violate the
46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         {@link Comparable} contract
46551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
46651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(Object[] a) {
46751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (LegacyMergeSort.userRequested)
46851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            legacyMergeSort(a);
46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
47051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            ComparableTimSort.sort(a);
47151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
47251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /** To be removed in a future release. */
47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void legacyMergeSort(Object[] a) {
47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        Object[] aux = a.clone();
47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(aux, a, 0, a.length, 0);
47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the specified array of objects into
48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending order, according to the
48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@linkplain Comparable natural ordering} of its
48351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements.  The range to be sorted extends from index
48451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
48551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (If {@code fromIndex==toIndex}, the range to be sorted is empty.)  All
48651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements in this range must implement the {@link Comparable}
48751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interface.  Furthermore, all elements in this range must be <i>mutually
48851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a
48951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code ClassCastException} for any elements {@code e1} and
49051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code e2} in the array).
49151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
49251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
49351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not be reordered as a result of the sort.
49451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
49551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: This implementation is a stable, adaptive,
49651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * iterative mergesort that requires far fewer than n lg(n) comparisons
49751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * when the input array is partially sorted, while offering the
49851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * performance of a traditional mergesort when the input array is
49951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * randomly ordered.  If the input array is nearly sorted, the
50051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation requires approximately n comparisons.  Temporary
50151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * storage requirements vary from a small constant for nearly sorted
50251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input arrays to n/2 object references for randomly ordered input
50351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
50551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation takes equal advantage of ascending and
50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * descending order in its input array, and can take advantage of
50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending and descending order in different parts of the the same
50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input array.  It is well-suited to merging two or more sorted arrays:
50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * simply concatenate the arrays and sort the resulting array.
51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation was adapted from Tim Peters's list sort for Python
51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting and Information Theoretic Complexity", in Proceedings of the
51551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * January 1993.
51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
51951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
52051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        sorted
52151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be sorted
52251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
52351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         (optional) if the natural ordering of the array elements is
52451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         found to violate the {@link Comparable} contract
52551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
52651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         {@code toIndex > a.length}
52751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are
52851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         not <i>mutually comparable</i> (for example, strings and
52951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         integers).
53051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
53151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(Object[] a, int fromIndex, int toIndex) {
53251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (LegacyMergeSort.userRequested)
53351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            legacyMergeSort(a, fromIndex, toIndex);
53451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
53551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            ComparableTimSort.sort(a, fromIndex, toIndex);
53651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
53751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
53851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /** To be removed in a future release. */
53951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void legacyMergeSort(Object[] a,
54051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                        int fromIndex, int toIndex) {
54151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
54251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        Object[] aux = copyOfRange(a, fromIndex, toIndex);
54351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
54451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
54551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
54651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
54751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Tuning parameter: list size at or below which insertion sort will be
54851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * used in preference to mergesort.
54951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * To be removed in a future release.
55051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
55151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static final int INSERTIONSORT_THRESHOLD = 7;
55251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
55351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
55451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Src is the source array that starts at index 0
55551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Dest is the (possibly larger) array destination with a possible offset
55651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * low is the index in dest to start sorting
55751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * high is the end index in dest to end sorting
55851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * off is the offset to generate corresponding low, high in src
55951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * To be removed in a future release.
56051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
56151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void mergeSort(Object[] src,
56251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  Object[] dest,
56351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  int low,
56451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  int high,
56551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  int off) {
56651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = high - low;
56751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
56851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Insertion sort on smallest arrays
56951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (length < INSERTIONSORT_THRESHOLD) {
57051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            for (int i=low; i<high; i++)
57151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                for (int j=i; j>low &&
57251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
57351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    swap(dest, j, j-1);
57451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
57551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
57651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
57751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Recursively sort halves of dest into src
57851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int destLow  = low;
57951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int destHigh = high;
58051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        low  += off;
58151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        high += off;
58251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int mid = (low + high) >>> 1;
58351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(dest, src, low, mid, -off);
58451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(dest, src, mid, high, -off);
58551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
58651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // If list is already sorted, just copy from src to dest.  This is an
58751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // optimization that results in faster sorts for nearly ordered lists.
58851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
58951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            System.arraycopy(src, low, dest, destLow, length);
59051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
59151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
59251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
59351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Merge sorted halves (now in src) into dest
59451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
59551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
59651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                dest[i] = src[p++];
59751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
59851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                dest[i] = src[q++];
59951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
60051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
60151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
60251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
60351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Swaps x[a] with x[b].
60451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void swap(Object[] x, int a, int b) {
60651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        Object t = x[a];
60751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        x[a] = x[b];
60851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        x[b] = t;
60951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
61051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
61151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
61251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array of objects according to the order induced by
61351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified comparator.  All elements in the array must be
61451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>mutually comparable</i> by the specified comparator (that is,
61551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
61651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * for any elements {@code e1} and {@code e2} in the array).
61751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
61851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
61951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not be reordered as a result of the sort.
62051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
62151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: This implementation is a stable, adaptive,
62251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * iterative mergesort that requires far fewer than n lg(n) comparisons
62351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * when the input array is partially sorted, while offering the
62451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * performance of a traditional mergesort when the input array is
62551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * randomly ordered.  If the input array is nearly sorted, the
62651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation requires approximately n comparisons.  Temporary
62751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * storage requirements vary from a small constant for nearly sorted
62851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input arrays to n/2 object references for randomly ordered input
62951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
63051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
63151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation takes equal advantage of ascending and
63251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * descending order in its input array, and can take advantage of
63351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending and descending order in different parts of the the same
63451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input array.  It is well-suited to merging two or more sorted arrays:
63551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * simply concatenate the arrays and sort the resulting array.
63651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
63751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation was adapted from Tim Peters's list sort for Python
63851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
63951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting and Information Theoretic Complexity", in Proceedings of the
64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * January 1993.
64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param c the comparator to determine the order of the array.  A
64651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@code null} value indicates that the elements'
64751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@linkplain Comparable natural ordering} should be used.
64851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are
64951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         not <i>mutually comparable</i> using the specified comparator
65051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException (optional) if the comparator is
65151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         found to violate the {@link Comparator} contract
65251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> void sort(T[] a, Comparator<? super T> c) {
65451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (LegacyMergeSort.userRequested)
65551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            legacyMergeSort(a, c);
65651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
65751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            TimSort.sort(a, c);
65851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
65951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
66051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /** To be removed in a future release. */
66151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {
66251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        T[] aux = a.clone();
66351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (c==null)
66451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            mergeSort(aux, a, 0, a.length, 0);
66551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
66651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            mergeSort(aux, a, 0, a.length, 0, c);
66751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
66851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
66951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
67051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the specified array of objects according
67151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to the order induced by the specified comparator.  The range to be
67251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * sorted extends from index {@code fromIndex}, inclusive, to index
67351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
67451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be sorted is empty.)  All elements in the range must be
67551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>mutually comparable</i> by the specified comparator (that is,
67651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
67751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * for any elements {@code e1} and {@code e2} in the range).
67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not be reordered as a result of the sort.
68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: This implementation is a stable, adaptive,
68351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * iterative mergesort that requires far fewer than n lg(n) comparisons
68451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * when the input array is partially sorted, while offering the
68551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * performance of a traditional mergesort when the input array is
68651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * randomly ordered.  If the input array is nearly sorted, the
68751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation requires approximately n comparisons.  Temporary
68851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * storage requirements vary from a small constant for nearly sorted
68951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input arrays to n/2 object references for randomly ordered input
69051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
69151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
69251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation takes equal advantage of ascending and
69351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * descending order in its input array, and can take advantage of
69451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending and descending order in different parts of the the same
69551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input array.  It is well-suited to merging two or more sorted arrays:
69651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * simply concatenate the arrays and sort the resulting array.
69751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
69851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation was adapted from Tim Peters's list sort for Python
69951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
70051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
70151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting and Information Theoretic Complexity", in Proceedings of the
70251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
70351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * January 1993.
70451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
70551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
70651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
70751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        sorted
70851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be sorted
70951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param c the comparator to determine the order of the array.  A
71051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@code null} value indicates that the elements'
71151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@linkplain Comparable natural ordering} should be used.
71251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are not
71351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>mutually comparable</i> using the specified comparator.
71451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
71551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         (optional) if the comparator is found to violate the
71651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         {@link Comparator} contract
71751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
71851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         {@code toIndex > a.length}
71951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
72051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> void sort(T[] a, int fromIndex, int toIndex,
72151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                Comparator<? super T> c) {
72251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (LegacyMergeSort.userRequested)
72351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            legacyMergeSort(a, fromIndex, toIndex, c);
72451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
72551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            TimSort.sort(a, fromIndex, toIndex, c);
72651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
72751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
72851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /** To be removed in a future release. */
72951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static <T> void legacyMergeSort(T[] a, int fromIndex, int toIndex,
73051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                            Comparator<? super T> c) {
73151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
73251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        T[] aux = copyOfRange(a, fromIndex, toIndex);
73351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (c==null)
73451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
73551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
73651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c);
73751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
73851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
73951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
74051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Src is the source array that starts at index 0
74151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Dest is the (possibly larger) array destination with a possible offset
74251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * low is the index in dest to start sorting
74351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * high is the end index in dest to end sorting
74451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * off is the offset into src corresponding to low in dest
74551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * To be removed in a future release.
74651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
74751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void mergeSort(Object[] src,
74851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  Object[] dest,
74951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  int low, int high, int off,
75051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  Comparator c) {
75151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = high - low;
75251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
75351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Insertion sort on smallest arrays
75451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (length < INSERTIONSORT_THRESHOLD) {
75551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            for (int i=low; i<high; i++)
75651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
75751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    swap(dest, j, j-1);
75851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
76051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
76151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Recursively sort halves of dest into src
76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int destLow  = low;
76351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int destHigh = high;
76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        low  += off;
76551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        high += off;
76651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int mid = (low + high) >>> 1;
76751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(dest, src, low, mid, -off, c);
76851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(dest, src, mid, high, -off, c);
76951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
77051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // If list is already sorted, just copy from src to dest.  This is an
77151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // optimization that results in faster sorts for nearly ordered lists.
77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (c.compare(src[mid-1], src[mid]) <= 0) {
77351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski           System.arraycopy(src, low, dest, destLow, length);
77451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski           return;
77551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
77651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
77751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Merge sorted halves (now in src) into dest
77851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
77951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
78051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                dest[i] = src[p++];
78151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                dest[i] = src[q++];
78351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
78451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
78551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
78651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
78751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Checks that {@code fromIndex} and {@code toIndex} are in
78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range and throws an appropriate exception, if they aren't.
78951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
79051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void rangeCheck(int length, int fromIndex, int toIndex) {
79151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (fromIndex > toIndex) {
79251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(
79351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (fromIndex < 0) {
79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new ArrayIndexOutOfBoundsException(fromIndex);
79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
79851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (toIndex > length) {
79951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new ArrayIndexOutOfBoundsException(toIndex);
80051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
80151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
80251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
80351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Searching
80451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
80551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
80651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of longs for the specified value using the
80751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.  The array must be sorted (as
80851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(long[])} method) prior to making this call.  If it
80951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the array contains
81051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
81151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
81251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
81351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
81451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
81551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
81651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
81751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
81851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
81951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
82051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
82151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
82251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
82351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
82451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(long[] a, long key) {
82551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
82651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
82751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
82851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
82951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
83051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of longs for the specified value using the
83151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.
83251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted (as
83351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(long[], int, int)} method)
83451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If it
83551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the range contains
83651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
83751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
83851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
83951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
84051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
84151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
84251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
84351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
84451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
84551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
84651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
84751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
84851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
84951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
85051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
85151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
85251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
85351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
85451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
85551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
85651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
85751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
85851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
85951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
86051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(long[] a, int fromIndex, int toIndex,
86151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   long key) {
86251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
86351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
86451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
86551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
86651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
86751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(long[] a, int fromIndex, int toIndex,
86851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     long key) {
86951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
87051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
87151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
87251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
87351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
87451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            long midVal = a[mid];
87551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
87651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
87751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
87851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
87951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
88051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
88151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
88251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
88351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
88451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
88551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
88651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
88751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of ints for the specified value using the
88851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.  The array must be sorted (as
88951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(int[])} method) prior to making this call.  If it
89051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the array contains
89151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
89251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
89351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
89451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
89551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
89651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
89751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
89851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
89951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
90051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
90151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
90251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
90351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
90451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
90551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(int[] a, int key) {
90651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
90751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
90851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
90951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
91051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
91151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of ints for the specified value using the
91251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.
91351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted (as
91451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(int[], int, int)} method)
91551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If it
91651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the range contains
91751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
91851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
91951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
92051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
92151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
92251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
92351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
92451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
92551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
92651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
92751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
92851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
92951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
93051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
93151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
93251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
93351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
93451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
93551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
93651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
93751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
93851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
93951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
94051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
94151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(int[] a, int fromIndex, int toIndex,
94251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   int key) {
94351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
94451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
94551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
94651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
94751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
94851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(int[] a, int fromIndex, int toIndex,
94951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     int key) {
95051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
95151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
95251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
95351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
95451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
95551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int midVal = a[mid];
95651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
95751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
95851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
95951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
96051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
96151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
96251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
96351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
96451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
96551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
96651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
96751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
96851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of shorts for the specified value using
96951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.  The array must be sorted
97051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(short[])} method) prior to making this call.  If
97151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * it is not sorted, the results are undefined.  If the array contains
97251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
97351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
97451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
97551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
97651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
97751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
97851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
97951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
98051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
98151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
98251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
98351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
98451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
98551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
98651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(short[] a, short key) {
98751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
98851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
98951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
99051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
99151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
99251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of shorts for the specified value using
99351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.
99451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted
99551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(short[], int, int)} method)
99651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If
99751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * it is not sorted, the results are undefined.  If the range contains
99851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
99951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
100051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
100151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
100251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
100351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
100451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
100551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
100651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
100751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
100851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
100951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
101051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
101151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
101251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
101351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
101451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
101551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
101651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
101751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
101851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
101951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
102051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
102151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
102251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(short[] a, int fromIndex, int toIndex,
102351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   short key) {
102451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
102551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
102651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
102751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
102851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
102951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(short[] a, int fromIndex, int toIndex,
103051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     short key) {
103151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
103251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
103351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
103451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
103551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
103651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            short midVal = a[mid];
103751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
103851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
103951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
104051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
104151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
104251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
104351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
104451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
104551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
104651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
104751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
104851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
104951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of chars for the specified value using the
105051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.  The array must be sorted (as
105151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(char[])} method) prior to making this call.  If it
105251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the array contains
105351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
105451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
105551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
105651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
105751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
105851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
105951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
106051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
106151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
106251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
106351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
106451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
106551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
106651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
106751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(char[] a, char key) {
106851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
106951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
107051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
107151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
107251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
107351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of chars for the specified value using the
107451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.
107551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted (as
107651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(char[], int, int)} method)
107751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If it
107851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the range contains
107951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
108051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
108151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
108251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
108351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
108451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
108551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
108651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
108751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
108851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
108951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
109051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
109151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
109251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
109351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
109451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
109551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
109651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
109751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
109851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
109951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
110051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
110151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
110251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
110351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(char[] a, int fromIndex, int toIndex,
110451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   char key) {
110551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
110651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
110751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
110851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
110951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
111051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(char[] a, int fromIndex, int toIndex,
111151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     char key) {
111251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
111351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
111451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
111551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
111651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
111751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            char midVal = a[mid];
111851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
111951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
112051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
112151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
112251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
112351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
112451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
112551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
112651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
112751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
112851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
112951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
113051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of bytes for the specified value using the
113151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.  The array must be sorted (as
113251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(byte[])} method) prior to making this call.  If it
113351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the array contains
113451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
113551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
113651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
113751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
113851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
113951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
114051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
114151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
114251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
114351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
114451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
114551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
114651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
114751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
114851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(byte[] a, byte key) {
114951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
115051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
115151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
115251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
115351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
115451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of bytes for the specified value using the
115551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.
115651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted (as
115751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(byte[], int, int)} method)
115851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If it
115951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the range contains
116051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
116151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
116251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
116351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
116451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
116551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
116651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
116751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
116851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
116951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
117051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
117151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
117251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
117351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
117451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
117551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
117651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
117751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
117851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
117951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
118051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
118151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
118251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
118351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
118451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(byte[] a, int fromIndex, int toIndex,
118551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   byte key) {
118651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
118751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
118851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
118951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
119051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
119151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
119251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     byte key) {
119351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
119451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
119551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
119651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
119751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
119851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            byte midVal = a[mid];
119951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
120251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of doubles for the specified value using
121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.  The array must be sorted
121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(double[])} method) prior to making this call.
121451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If it is not sorted, the results are undefined.  If the array contains
121551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
121651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.  This method considers all NaN values to be
121751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equivalent and equal.
121851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
121951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
122051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
122151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
122251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
122351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
122451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
122551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
122651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
122751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
122851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
122951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
123051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(double[] a, double key) {
123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
123251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
123351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
123451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
123551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
123651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of doubles for the specified value using
123751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.
123851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted
123951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(double[], int, int)} method)
124051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.
124151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If it is not sorted, the results are undefined.  If the range contains
124251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
124351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.  This method considers all NaN values to be
124451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equivalent and equal.
124551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
124651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
124751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
124851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
124951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
125051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
125151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
125251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
125351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
125451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
125551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
125651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
125751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
125851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
125951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
126051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
126151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
126251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
126351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
126451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
126551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
126651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
126751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(double[] a, int fromIndex, int toIndex,
126851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   double key) {
126951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
127051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
127151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
127251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
127351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
127451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(double[] a, int fromIndex, int toIndex,
127551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     double key) {
127651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
127751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
127851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
127951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
128051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
128151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            double midVal = a[mid];
128251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
128351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
128451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;  // Neither val is NaN, thisVal is smaller
128551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
128651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1; // Neither val is NaN, thisVal is larger
128751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else {
128851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                long midBits = Double.doubleToLongBits(midVal);
128951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                long keyBits = Double.doubleToLongBits(key);
129051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                if (midBits == keyBits)     // Values are equal
129151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    return mid;             // Key found
129251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
129351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    low = mid + 1;
129451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                else                        // (0.0, -0.0) or (NaN, !NaN)
129551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    high = mid - 1;
129651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
129751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
129851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
129951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
130051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
130151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
130251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of floats for the specified value using
130351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm. The array must be sorted
130451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(float[])} method) prior to making this call. If
130551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * it is not sorted, the results are undefined. If the array contains
130651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
130751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found. This method considers all NaN values to be
130851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equivalent and equal.
130951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
131051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
131151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
131251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
131351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
131451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
131551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
131651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
131751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key. Note
131851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
131951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
132051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
132151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(float[] a, float key) {
132251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
132351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
132451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
132551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
132651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
132751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of floats for the specified value using
132851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.
132951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted
133051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(float[], int, int)} method)
133151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call. If
133251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * it is not sorted, the results are undefined. If the range contains
133351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
133451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found. This method considers all NaN values to be
133551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equivalent and equal.
133651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
133751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
133851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
133951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
134051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
134151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
134251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
134351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
134451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
134551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
134651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
134751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
134851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
134951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key. Note
135051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
135151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
135251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
135351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
135451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
135551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
135651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
135751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
135851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(float[] a, int fromIndex, int toIndex,
135951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   float key) {
136051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
136151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
136251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
136351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
136451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
136551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(float[] a, int fromIndex, int toIndex,
136651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     float key) {
136751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
136851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
136951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
137051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
137151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
137251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            float midVal = a[mid];
137351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
137451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
137551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;  // Neither val is NaN, thisVal is smaller
137651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
137751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1; // Neither val is NaN, thisVal is larger
137851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else {
137951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                int midBits = Float.floatToIntBits(midVal);
138051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                int keyBits = Float.floatToIntBits(key);
138151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                if (midBits == keyBits)     // Values are equal
138251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    return mid;             // Key found
138351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
138451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    low = mid + 1;
138551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                else                        // (0.0, -0.0) or (NaN, !NaN)
138651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    high = mid - 1;
138751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
138851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
138951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
139051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
139151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
139251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
139351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array for the specified object using the binary
139451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * search algorithm. The array must be sorted into ascending order
139551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * according to the
139651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@linkplain Comparable natural ordering}
139751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of its elements (as by the
139851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link #sort(Object[])} method) prior to making this call.
139951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If it is not sorted, the results are undefined.
140051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (If the array contains elements that are not mutually comparable (for
140151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * example, strings and integers), it <i>cannot</i> be sorted according
140251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to the natural ordering of its elements, hence results are undefined.)
140351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the array contains multiple
140451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements equal to the specified object, there is no guarantee which
140551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
140651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
140751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
140851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
140951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
141051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
141151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
141251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
141351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
141451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
141551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
141651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
141751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the search key is not comparable to the
141851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements of the array.
141951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
142051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(Object[] a, Object key) {
142151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
142251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
142351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
142451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
142551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
142651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array for the specified object using the binary
142751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * search algorithm.
142851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted into ascending order
142951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * according to the
143051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@linkplain Comparable natural ordering}
143151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of its elements (as by the
143251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link #sort(Object[], int, int)} method) prior to making this
143351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * call.  If it is not sorted, the results are undefined.
143451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (If the range contains elements that are not mutually comparable (for
143551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * example, strings and integers), it <i>cannot</i> be sorted according
143651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to the natural ordering of its elements, hence results are undefined.)
143751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the range contains multiple
143851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements equal to the specified object, there is no guarantee which
143951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
144051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
144151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
144251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
144351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
144451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
144551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
144651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
144751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
144851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
144951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
145051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
145151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
145251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
145351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
145451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
145551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
145651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the search key is not comparable to the
145751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements of the array within the specified range.
145851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
145951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
146051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
146151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
146251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
146351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
146451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(Object[] a, int fromIndex, int toIndex,
146551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   Object key) {
146651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
146751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
146851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
146951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
147051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
147151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
147251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     Object key) {
147351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
147451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
147551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
147651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
147751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
147851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Comparable midVal = (Comparable)a[mid];
147951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int cmp = midVal.compareTo(key);
148051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
148151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (cmp < 0)
148251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
148351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (cmp > 0)
148451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
148551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
148651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
148751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
148851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
148951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
149051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
149151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
149251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array for the specified object using the binary
149351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * search algorithm.  The array must be sorted into ascending order
149451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * according to the specified comparator (as by the
149551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link #sort(Object[], Comparator) sort(T[], Comparator)}
149651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method) prior to making this call.  If it is
149751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not sorted, the results are undefined.
149851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the array contains multiple
149951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements equal to the specified object, there is no guarantee which one
150051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * will be found.
150151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
150251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
150351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
150451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param c the comparator by which the array is ordered.  A
150551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        <tt>null</tt> value indicates that the elements'
150651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@linkplain Comparable natural ordering} should be used.
150751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
150851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
150951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
151051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
151151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
151251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
151351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
151451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
151551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are not
151651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>mutually comparable</i> using the specified comparator,
151751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or the search key is not comparable to the
151851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements of the array using this comparator.
151951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
152051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
152151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key, c);
152251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
152351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
152451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
152551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
152651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array for the specified object using the binary
152751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * search algorithm.
152851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted into ascending order
152951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * according to the specified comparator (as by the
153051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link #sort(Object[], int, int, Comparator)
153151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * sort(T[], int, int, Comparator)}
153251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method) prior to making this call.
153351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If it is not sorted, the results are undefined.
153451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the range contains multiple elements equal to the specified object,
153551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * there is no guarantee which one will be found.
153651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
153751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
153851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
153951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
154051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
154151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
154251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param c the comparator by which the array is ordered.  A
154351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        <tt>null</tt> value indicates that the elements'
154451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@linkplain Comparable natural ordering} should be used.
154551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
154651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
154751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
154851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
154951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
155051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
155151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
155251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
155351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
155451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
155551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the range contains elements that are not
155651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>mutually comparable</i> using the specified comparator,
155751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or the search key is not comparable to the
155851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range using this comparator.
155951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
156051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
156151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
156251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
156351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
156451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
156551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
156651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                       T key, Comparator<? super T> c) {
156751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
156851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key, c);
156951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
157051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
157151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
157251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
157351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                         T key, Comparator<? super T> c) {
157451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (c == null) {
157551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return binarySearch0(a, fromIndex, toIndex, key);
157651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
157751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
157851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
157951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
158051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
158151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
158251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            T midVal = a[mid];
158351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int cmp = c.compare(midVal, key);
158451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (cmp < 0)
158551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
158651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (cmp > 0)
158751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
158851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
158951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
159051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
159151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
159251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
159351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
159451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Equality Testing
159551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
159651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
159751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of longs are
159851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
159951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
160051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
160151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
160251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
160351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
160451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
160551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
160651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
160751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
160851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(long[] a, long[] a2) {
160951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
161051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
161151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
161251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
161351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
161451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
161551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
161651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
161751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
161851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
161951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
162051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
162151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
162251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
162351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
162451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
162551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
162651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of ints are
162751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
162851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
162951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
163051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
163151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
163251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
163351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
163451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
163551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
163651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
163751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(int[] a, int[] a2) {
163851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
163951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
164051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
164151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
164251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
164351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
164451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
164551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
164651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
164751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
164851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
164951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
165051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
165151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
165251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
165351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
165451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
165551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of shorts are
165651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
165751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
165851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
165951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
166051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
166151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
166251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
166351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
166451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
166551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
166651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(short[] a, short a2[]) {
166751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
166851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
166951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
167051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
167151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
167251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
167351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
167451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
167551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
167651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
167751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
167851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
167951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
168051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
168151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
168251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
168351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
168451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of chars are
168551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
168651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
168751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
168851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
168951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
169051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
169151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
169251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
169351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
169451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
169551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(char[] a, char[] a2) {
169651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
169751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
169851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
169951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
170051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
170151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
170251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
170351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
170451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
170551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
170651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
170751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
170851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
170951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
171051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
171151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
171251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
171351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of bytes are
171451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
171551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
171651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
171751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
171851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
171951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
172051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
172151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
172251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
172351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
172451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(byte[] a, byte[] a2) {
172551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
172651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
172751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
172851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
172951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
173051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
173151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
173251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
173351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
173451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
173551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
173651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
173751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
173851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
173951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
174051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
174151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
174251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of booleans are
174351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
174451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
174551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
174651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
174751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
174851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
174951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
175051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
175151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
175251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
175351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(boolean[] a, boolean[] a2) {
175451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
175551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
175651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
175751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
175851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
175951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
176051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
176151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
176251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
176351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
176451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
176551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
176651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
176751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
176851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
176951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
177051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
177151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of doubles are
177251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
177351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
177451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
177551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
177651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
177751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
177851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
177951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <pre>    <tt>new Double(d1).equals(new Double(d2))</tt></pre>
178051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (Unlike the <tt>==</tt> operator, this method considers
178151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
178251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
178351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
178451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
178551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
178651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see Double#equals(Object)
178751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
178851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(double[] a, double[] a2) {
178951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
179051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
179151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
179251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
179351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
179451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
179551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
179651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
179751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
179851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
179951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
180051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
180151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
180251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
180351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
180451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
180551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
180651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of floats are
180751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
180851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
180951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
181051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
181151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
181251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
181351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Two floats <tt>f1</tt> and <tt>f2</tt> are considered equal if:
181451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <pre>    <tt>new Float(f1).equals(new Float(f2))</tt></pre>
181551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (Unlike the <tt>==</tt> operator, this method considers
181651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>NaN</tt> equals to itself, and 0.0f unequal to -0.0f.)
181751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
181851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
181951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
182051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
182151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see Float#equals(Object)
182251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
182351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(float[] a, float[] a2) {
182451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
182551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
182651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
182751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
182851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
182951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
183051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
183151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
183251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
183351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
183451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
183551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
183651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
183751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
183851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
183951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
184051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
184151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of Objects are
184251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  The two arrays are considered equal if
184351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * both arrays contain the same number of elements, and all corresponding
184451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * pairs of elements in the two arrays are equal.  Two objects <tt>e1</tt>
184551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
184651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * : e1.equals(e2))</tt>.  In other words, the two arrays are equal if
184751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * they contain the same elements in the same order.  Also, two array
184851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * references are considered equal if both are <tt>null</tt>.<p>
184951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
185051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
185151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
185251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
185351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
185451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(Object[] a, Object[] a2) {
185551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
185651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
185751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
185851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
185951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
186051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
186151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
186251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
186351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
186451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++) {
186551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object o1 = a[i];
186651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object o2 = a2[i];
186751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (!(o1==null ? o2==null : o1.equals(o2)))
186851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
186951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
187051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
187151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
187251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
187351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
187451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Filling
187551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
187651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
187751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified long value to each element of the specified array
187851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of longs.
187951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
188051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
188151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
188251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
188351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(long[] a, long val) {
188451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
188551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
188651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
188751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
188851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
188951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified long value to each element of the specified
189051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of longs.  The range to be filled
189151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
189251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
189351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
189451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
189551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
189651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
189751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
189851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
189951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
190051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
190151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
190251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
190351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
190451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
190551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(long[] a, int fromIndex, int toIndex, long val) {
190651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
190751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
190851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
190951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
191051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
191151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
191251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified int value to each element of the specified array
191351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of ints.
191451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
191551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
191651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
191751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
191851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(int[] a, int val) {
191951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
192051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
192151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
192251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
192351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
192451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified int value to each element of the specified
192551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of ints.  The range to be filled
192651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
192751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
192851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
192951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
193051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
193151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
193251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
193351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
193451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
193551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
193651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
193751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
193851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
193951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
194051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(int[] a, int fromIndex, int toIndex, int val) {
194151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
194251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
194351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
194451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
194551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
194651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
194751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified short value to each element of the specified array
194851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of shorts.
194951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
195051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
195151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
195251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
195351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(short[] a, short val) {
195451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
195551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
195651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
195751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
195851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
195951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified short value to each element of the specified
196051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of shorts.  The range to be filled
196151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
196251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
196351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
196451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
196551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
196651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
196751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
196851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
196951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
197051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
197151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
197251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
197351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
197451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
197551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(short[] a, int fromIndex, int toIndex, short val) {
197651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
197751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
197851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
197951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
198051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
198151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
198251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified char value to each element of the specified array
198351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of chars.
198451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
198551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
198651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
198751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
198851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(char[] a, char val) {
198951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
199051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
199151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
199251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
199351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
199451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified char value to each element of the specified
199551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of chars.  The range to be filled
199651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
199751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
199851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
199951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
200051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
200151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
200251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
200351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
200451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
200551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
200651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
200751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
200851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
200951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
201051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(char[] a, int fromIndex, int toIndex, char val) {
201151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
201251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
201351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
201451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
201551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
201651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
201751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified byte value to each element of the specified array
201851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of bytes.
201951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
202051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
202151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
202251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
202351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(byte[] a, byte val) {
202451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
202551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
202651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
202751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
202851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
202951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified byte value to each element of the specified
203051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of bytes.  The range to be filled
203151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
203251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
203351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
203451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
203551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
203651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
203751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
203851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
203951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
204051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
204151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
204251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
204351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
204451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
204551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
204651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
204751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
204851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
204951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
205051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
205151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
205251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified boolean value to each element of the specified
205351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array of booleans.
205451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
205551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
205651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
205751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
205851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(boolean[] a, boolean val) {
205951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
206051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
206151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
206251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
206351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
206451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified boolean value to each element of the specified
206551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of booleans.  The range to be filled
206651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
206751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
206851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
206951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
207051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
207151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
207251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
207351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
207451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
207551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
207651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
207751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
207851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
207951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
208051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(boolean[] a, int fromIndex, int toIndex,
208151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                            boolean val) {
208251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
208351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
208451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
208551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
208651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
208751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
208851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified double value to each element of the specified
208951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array of doubles.
209051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
209151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
209251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
209351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
209451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(double[] a, double val) {
209551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
209651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
209751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
209851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
209951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
210051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified double value to each element of the specified
210151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of doubles.  The range to be filled
210251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
210351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
210451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
210551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
210651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
210751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
210851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
210951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
211051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
211151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
211251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
211351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
211451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
211551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
211651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(double[] a, int fromIndex, int toIndex,double val){
211751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
211851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
211951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
212051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
212151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
212251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
212351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified float value to each element of the specified array
212451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of floats.
212551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
212651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
212751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
212851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
212951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(float[] a, float val) {
213051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
213151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
213251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
213351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
213451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
213551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified float value to each element of the specified
213651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of floats.  The range to be filled
213751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
213851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
213951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
214051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
214151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
214251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
214351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
214451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
214551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
214651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
214751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
214851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
214951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
215051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
215151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(float[] a, int fromIndex, int toIndex, float val) {
215251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
215351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
215451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
215551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
215651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
215751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
215851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified Object reference to each element of the specified
215951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array of Objects.
216051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
216151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
216251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
216351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayStoreException if the specified value is not of a
216451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         runtime type that can be stored in the specified array
216551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
216651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(Object[] a, Object val) {
216751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
216851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
216951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
217051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
217151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
217251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified Object reference to each element of the specified
217351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of Objects.  The range to be filled
217451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
217551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
217651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
217751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
217851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
217951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
218051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
218151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
218251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
218351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
218451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
218551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
218651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
218751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayStoreException if the specified value is not of a
218851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         runtime type that can be stored in the specified array
218951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
219051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
219151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
219251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
219351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
219451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
219551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
219651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Cloning
219751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
219851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
219951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with nulls (if necessary)
220051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
220151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
220251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
220351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>null</tt>.
220451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
220551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
220651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The resulting array is of exactly the same class as the original array.
220751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
220851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
220951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
221051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with nulls
221151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
221251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
221351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
221451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
221551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
221651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> T[] copyOf(T[] original, int newLength) {
221751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return (T[]) copyOf(original, newLength, original.getClass());
221851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
221951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
222051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
222151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with nulls (if necessary)
222251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
222351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
222451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
222551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>null</tt>.
222651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
222751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
222851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The resulting array is of the class <tt>newType</tt>.
222951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
223051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
223151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
223251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newType the class of the copy to be returned
223351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with nulls
223451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
223551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
223651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
223751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayStoreException if an element copied from
223851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     <tt>original</tt> is not of a runtime type that can be stored in
223951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     an array of class <tt>newType</tt>
224051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
224151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
224251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
224351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        T[] copy = ((Object)newType == (Object)Object[].class)
224451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            ? (T[]) new Object[newLength]
224551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
224651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
224751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
224851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
224951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
225051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
225151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
225251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
225351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
225451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
225551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
225651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>(byte)0</tt>.
225751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
225851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
225951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
226051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
226151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
226251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
226351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
226451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
226551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
226651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
226751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
226851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static byte[] copyOf(byte[] original, int newLength) {
226951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        byte[] copy = new byte[newLength];
227051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
227151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
227251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
227351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
227451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
227551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
227651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
227751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
227851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
227951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
228051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>(short)0</tt>.
228151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
228251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
228351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
228451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
228551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
228651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
228751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
228851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
228951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
229051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
229151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
229251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static short[] copyOf(short[] original, int newLength) {
229351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        short[] copy = new short[newLength];
229451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
229551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
229651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
229751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
229851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
229951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
230051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
230151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
230251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
230351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
230451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>0</tt>.
230551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
230651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
230751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
230851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
230951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
231051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
231151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
231251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
231351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
231451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
231551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
231651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int[] copyOf(int[] original, int newLength) {
231751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int[] copy = new int[newLength];
231851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
231951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
232051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
232151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
232251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
232351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
232451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
232551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
232651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
232751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
232851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>0L</tt>.
232951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
233051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
233151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
233251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
233351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
233451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
233551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
233651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
233751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
233851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
233951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
234051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static long[] copyOf(long[] original, int newLength) {
234151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        long[] copy = new long[newLength];
234251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
234351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
234451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
234551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
234651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
234751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
234851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with null characters (if necessary)
234951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are valid
235051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * in both the original array and the copy, the two arrays will contain
235151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * identical values.  For any indices that are valid in the copy but not
235251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the original, the copy will contain <tt>'\\u000'</tt>.  Such indices
235351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * will exist if and only if the specified length is greater than that of
235451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the original array.
235551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
235651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
235751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
235851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with null characters
235951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
236051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
236151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
236251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
236351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
236451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static char[] copyOf(char[] original, int newLength) {
236551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        char[] copy = new char[newLength];
236651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
236751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
236851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
236951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
237051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
237151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
237251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
237351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
237451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
237551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
237651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>0f</tt>.
237751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
237851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
237951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
238051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
238151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
238251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
238351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
238451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
238551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
238651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
238751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
238851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static float[] copyOf(float[] original, int newLength) {
238951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        float[] copy = new float[newLength];
239051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
239151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
239251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
239351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
239451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
239551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
239651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
239751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
239851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
239951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
240051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>0d</tt>.
240151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
240251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
240351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
240451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
240551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
240651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
240751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
240851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
240951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
241051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
241151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
241251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static double[] copyOf(double[] original, int newLength) {
241351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        double[] copy = new double[newLength];
241451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
241551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
241651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
241751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
241851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
241951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
242051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with <tt>false</tt> (if necessary)
242151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
242251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
242351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
242451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>false</tt>.
242551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
242651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
242751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
242851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
242951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
243051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with false elements
243151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
243251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
243351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
243451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
243551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
243651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean[] copyOf(boolean[] original, int newLength) {
243751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        boolean[] copy = new boolean[newLength];
243851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
243951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
244051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
244151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
244251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
244351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
244451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
244551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
244651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
244751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
244851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
244951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
245051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
245151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
245251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
245351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>null</tt> is placed in all elements of the copy whose index is
245451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
245551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
245651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>
245751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The resulting array is of exactly the same class as the original array.
245851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
245951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
246051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
246151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
246251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
246351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
246451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with nulls to obtain the required length
246551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
246651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
246751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
246851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
246951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
247051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
247151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> T[] copyOfRange(T[] original, int from, int to) {
247251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copyOfRange(original, from, to, (Class<T[]>) original.getClass());
247351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
247451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
247551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
247651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
247751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
247851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
247951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
248051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
248151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
248251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
248351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
248451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
248551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>null</tt> is placed in all elements of the copy whose index is
248651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
248751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
248851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The resulting array is of the class <tt>newType</tt>.
248951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
249051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
249151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
249251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
249351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
249451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newType the class of the copy to be returned
249551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
249651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with nulls to obtain the required length
249751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
249851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
249951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
250051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
250151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayStoreException if an element copied from
250251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     <tt>original</tt> is not of a runtime type that can be stored in
250351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     an array of class <tt>newType</tt>.
250451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
250551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
250651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
250751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
250851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
250951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
251051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        T[] copy = ((Object)newType == (Object)Object[].class)
251151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            ? (T[]) new Object[newLength]
251251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
251351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
251451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
251551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
251651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
251751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
251851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
251951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
252051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
252151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
252251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
252351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
252451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
252551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
252651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
252751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
252851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>(byte)0</tt> is placed in all elements of the copy whose index is
252951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
253051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
253151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
253251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
253351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
253451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
253551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
253651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
253751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
253851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
253951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
254051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
254151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
254251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
254351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
254451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static byte[] copyOfRange(byte[] original, int from, int to) {
254551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
254651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
254751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
254851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        byte[] copy = new byte[newLength];
254951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
255051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
255151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
255251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
255351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
255451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
255551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
255651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
255751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
255851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
255951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
256051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
256151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
256251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
256351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
256451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>(short)0</tt> is placed in all elements of the copy whose index is
256551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
256651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
256751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
256851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
256951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
257051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
257151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
257251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
257351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
257451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
257551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
257651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
257751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
257851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
257951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
258051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static short[] copyOfRange(short[] original, int from, int to) {
258151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
258251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
258351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
258451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        short[] copy = new short[newLength];
258551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
258651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
258751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
258851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
258951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
259051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
259151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
259251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
259351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
259451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
259551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
259651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
259751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
259851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
259951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
260051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>0</tt> is placed in all elements of the copy whose index is
260151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
260251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
260351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
260451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
260551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
260651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
260751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
260851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
260951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
261051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
261151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
261251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
261351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
261451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
261551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
261651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int[] copyOfRange(int[] original, int from, int to) {
261751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
261851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
261951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
262051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int[] copy = new int[newLength];
262151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
262251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
262351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
262451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
262551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
262651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
262751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
262851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
262951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
263051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
263151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
263251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
263351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
263451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
263551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
263651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>0L</tt> is placed in all elements of the copy whose index is
263751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
263851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
263951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
264051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
264151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
264251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
264351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
264451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
264551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
264651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
264751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
264851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
264951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
265051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
265151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
265251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static long[] copyOfRange(long[] original, int from, int to) {
265351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
265451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
265551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
265651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        long[] copy = new long[newLength];
265751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
265851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
265951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
266051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
266151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
266251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
266351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
266451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
266551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
266651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
266751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
266851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
266951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
267051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
267151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
267251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>'\\u000'</tt> is placed in all elements of the copy whose index is
267351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
267451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
267551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
267651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
267751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
267851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
267951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
268051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
268151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with null characters to obtain the required length
268251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
268351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
268451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
268551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
268651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
268751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
268851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static char[] copyOfRange(char[] original, int from, int to) {
268951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
269051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
269151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
269251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        char[] copy = new char[newLength];
269351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
269451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
269551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
269651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
269751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
269851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
269951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
270051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
270151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
270251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
270351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
270451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
270551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
270651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
270751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
270851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>0f</tt> is placed in all elements of the copy whose index is
270951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
271051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
271151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
271251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
271351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
271451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
271551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
271651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
271751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
271851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
271951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
272051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
272151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
272251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
272351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
272451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static float[] copyOfRange(float[] original, int from, int to) {
272551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
272651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
272751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
272851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        float[] copy = new float[newLength];
272951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
273051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
273151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
273251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
273351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
273451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
273551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
273651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
273751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
273851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
273951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
274051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
274151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
274251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
274351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
274451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>0d</tt> is placed in all elements of the copy whose index is
274551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
274651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
274751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
274851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
274951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
275051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
275151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
275251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
275351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
275451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
275551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
275651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
275751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
275851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
275951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
276051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static double[] copyOfRange(double[] original, int from, int to) {
276151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
276251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
276351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
276451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        double[] copy = new double[newLength];
276551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
276651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
276751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
276851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
276951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
277051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
277151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
277251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
277351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
277451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
277551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
277651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
277751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
277851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
277951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
278051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>false</tt> is placed in all elements of the copy whose index is
278151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
278251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
278351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
278451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
278551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
278651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
278751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
278851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
278951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with false elements to obtain the required length
279051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
279151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
279251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
279351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
279451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
279551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
279651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean[] copyOfRange(boolean[] original, int from, int to) {
279751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
279851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
279951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
280051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        boolean[] copy = new boolean[newLength];
280151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
280251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
280351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
280451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
280551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
280651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Misc
280751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
280851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
280951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a fixed-size list backed by the specified array.  (Changes to
281051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the returned list "write through" to the array.)  This method acts
281151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * as bridge between array-based and collection-based APIs, in
281251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * combination with {@link Collection#toArray}.  The returned list is
281351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * serializable and implements {@link RandomAccess}.
281451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
281551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This method also provides a convenient way to create a fixed-size
281651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * list initialized to contain several elements:
281751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <pre>
281851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
281951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </pre>
282051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
282151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array by which the list will be backed
282251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a list view of the specified array
282351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
282451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @SafeVarargs
282551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> List<T> asList(T... a) {
282651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return new ArrayList<>(a);
282751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
282851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
282951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
283051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @serial include
283151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
283251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static class ArrayList<E> extends AbstractList<E>
283351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        implements RandomAccess, java.io.Serializable
283451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    {
283551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        private static final long serialVersionUID = -2764017481108945198L;
283651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        private final E[] a;
283751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
283851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        ArrayList(E[] array) {
283951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (array==null)
284051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                throw new NullPointerException();
284151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a = array;
284251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
284351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
284451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public int size() {
284551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return a.length;
284651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
284751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
284851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public Object[] toArray() {
284951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return a.clone();
285051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
285151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
285251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public <T> T[] toArray(T[] a) {
285351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int size = size();
285451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a.length < size)
285551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return Arrays.copyOf(this.a, size,
285651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     (Class<? extends T[]>) a.getClass());
285751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            System.arraycopy(this.a, 0, a, 0, size);
285851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a.length > size)
285951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                a[size] = null;
286051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return a;
286151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
286251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
286351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public E get(int index) {
286451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return a[index];
286551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
286651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
286751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public E set(int index, E element) {
286851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            E oldValue = a[index];
286951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[index] = element;
287051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return oldValue;
287151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
287251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
287351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public int indexOf(Object o) {
287451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (o==null) {
287551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                for (int i=0; i<a.length; i++)
287651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    if (a[i]==null)
287751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        return i;
287851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            } else {
287951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                for (int i=0; i<a.length; i++)
288051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    if (o.equals(a[i]))
288151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        return i;
288251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
288351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return -1;
288451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
288551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
288651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public boolean contains(Object o) {
288751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return indexOf(o) != -1;
288851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
288951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
289051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
289151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
289251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
289351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>long</tt> arrays <tt>a</tt> and <tt>b</tt>
289451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
289551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
289651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
289751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
289851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
289951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Long}
290051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
290151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
290251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
290351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
290451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
290551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
290651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
290751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(long a[]) {
290851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
290951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
291051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
291151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
291251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (long element : a) {
291351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int elementHash = (int)(element ^ (element >>> 32));
291451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + elementHash;
291551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
291651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
291751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
291851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
291951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
292051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
292151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
292251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two non-null <tt>int</tt> arrays <tt>a</tt> and <tt>b</tt>
292351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
292451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
292551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
292651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
292751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
292851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Integer}
292951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
293051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
293151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
293251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
293351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
293451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
293551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
293651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(int a[]) {
293751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
293851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
293951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
294051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
294151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int element : a)
294251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + element;
294351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
294451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
294551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
294651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
294751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
294851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
294951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>short</tt> arrays <tt>a</tt> and <tt>b</tt>
295051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
295151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
295251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
295351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
295451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
295551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Short}
295651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
295751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
295851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
295951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
296051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
296151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
296251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
296351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(short a[]) {
296451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
296551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
296651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
296751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
296851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (short element : a)
296951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + element;
297051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
297151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
297251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
297351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
297451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
297551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
297651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>char</tt> arrays <tt>a</tt> and <tt>b</tt>
297751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
297851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
297951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
298051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
298151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
298251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Character}
298351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
298451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
298551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
298651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
298751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
298851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
298951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
299051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(char a[]) {
299151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
299251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
299351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
299451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
299551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (char element : a)
299651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + element;
299751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
299851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
299951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
300051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
300151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
300251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
300351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>byte</tt> arrays <tt>a</tt> and <tt>b</tt>
300451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
300551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
300651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
300751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
300851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
300951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Byte}
301051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
301151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
301251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
301351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
301451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
301551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
301651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
301751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(byte a[]) {
301851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
301951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
302051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
302151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
302251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (byte element : a)
302351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + element;
302451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
302551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
302651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
302751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
302851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
302951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
303051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>boolean</tt> arrays <tt>a</tt> and <tt>b</tt>
303151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
303251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
303351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
303451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
303551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
303651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Boolean}
303751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
303851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
303951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
304051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
304151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
304251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
304351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
304451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(boolean a[]) {
304551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
304651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
304751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
304851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
304951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (boolean element : a)
305051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + (element ? 1231 : 1237);
305151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
305251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
305351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
305451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
305551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
305651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
305751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>float</tt> arrays <tt>a</tt> and <tt>b</tt>
305851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
305951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
306051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
306151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
306251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
306351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Float}
306451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
306551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
306651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
306751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
306851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
306951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
307051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
307151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(float a[]) {
307251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
307351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
307451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
307551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
307651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (float element : a)
307751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + Float.floatToIntBits(element);
307851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
307951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
308051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
308151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
308251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
308351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
308451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>double</tt> arrays <tt>a</tt> and <tt>b</tt>
308551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
308651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
308751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
308851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
308951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
309051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Double}
309151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
309251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
309351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
309451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
309551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
309651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
309751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
309851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(double a[]) {
309951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
310051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
310151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
310251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
310351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (double element : a) {
310451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            long bits = Double.doubleToLongBits(element);
310551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + (int)(bits ^ (bits >>> 32));
310651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
310751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
310851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
310951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
311051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
311151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.  If
311251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the array contains other arrays as elements, the hash code is based on
311351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * their identities rather than their contents.  It is therefore
311451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * acceptable to invoke this method on an array that contains itself as an
311551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * element,  either directly or indirectly through one or more levels of
311651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
311751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
311851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
311951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.equals(a, b)</tt>, it is also the case that
312051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
312151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
312251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is equal to the value that would
312351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * be returned by <tt>Arrays.asList(a).hashCode()</tt>, unless <tt>a</tt>
312451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>, in which case <tt>0</tt> is returned.
312551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
312651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose content-based hash code to compute
312751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
312851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #deepHashCode(Object[])
312951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
313051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
313151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(Object a[]) {
313251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
313351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
313451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
313551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
313651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
313751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (Object element : a)
313851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + (element == null ? 0 : element.hashCode());
313951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
314051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
314151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
314251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
314351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
314451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the "deep contents" of the specified
314551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array.  If the array contains other arrays as elements, the
314651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * hash code is based on their contents and so on, ad infinitum.
314751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * It is therefore unacceptable to invoke this method on an array that
314851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contains itself as an element, either directly or indirectly through
314951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one or more levels of arrays.  The behavior of such an invocation is
315051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * undefined.
315151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
315251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
315351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.deepEquals(a, b)</tt>, it is also the case that
315451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.deepHashCode(a) == Arrays.deepHashCode(b)</tt>.
315551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
315651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The computation of the value returned by this method is similar to
315751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * that of the value returned by {@link List#hashCode()} on a list
315851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * containing the same elements as <tt>a</tt> in the same order, with one
315951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * difference: If an element <tt>e</tt> of <tt>a</tt> is itself an array,
316051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * its hash code is computed not by calling <tt>e.hashCode()</tt>, but as
316151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by calling the appropriate overloading of <tt>Arrays.hashCode(e)</tt>
316251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * if <tt>e</tt> is an array of a primitive type, or as by calling
316351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.deepHashCode(e)</tt> recursively if <tt>e</tt> is an array
316451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of a reference type.  If <tt>a</tt> is <tt>null</tt>, this method
316551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returns 0.
316651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
316751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose deep-content-based hash code to compute
316851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a deep-content-based hash code for <tt>a</tt>
316951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #hashCode(Object[])
317051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
317151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
317251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int deepHashCode(Object a[]) {
317351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
317451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
317551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
317651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
317751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
317851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (Object element : a) {
317951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int elementHash = 0;
3180669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            if (element != null) {
3181669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                Class<?> cl = element.getClass().getComponentType();
3182669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                if (cl == null)
3183669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = element.hashCode();
3184669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (element instanceof Object[])
3185669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = deepHashCode((Object[]) element);
3186669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == byte.class)
3187669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((byte[]) element);
3188669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == short.class)
3189669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((short[]) element);
3190669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == int.class)
3191669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((int[]) element);
3192669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == long.class)
3193669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((long[]) element);
3194669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == char.class)
3195669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((char[]) element);
3196669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == float.class)
3197669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((float[]) element);
3198669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == double.class)
3199669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((double[]) element);
3200669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == boolean.class)
3201669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((boolean[]) element);
3202669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else
3203669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = element.hashCode();
3204669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            }
320551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + elementHash;
320651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
320751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
320851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
320951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
321051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
321151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
321251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays are <i>deeply
321351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equal</i> to one another.  Unlike the {@link #equals(Object[],Object[])}
321451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method, this method is appropriate for use with nested arrays of
321551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arbitrary depth.
321651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
321751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Two array references are considered deeply equal if both
321851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are <tt>null</tt>, or if they refer to arrays that contain the same
321951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * number of elements and all corresponding pairs of elements in the two
322051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays are deeply equal.
322151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
322251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Two possibly <tt>null</tt> elements <tt>e1</tt> and <tt>e2</tt> are
322351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * deeply equal if any of the following conditions hold:
322451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <ul>
322551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    <li> <tt>e1</tt> and <tt>e2</tt> are both arrays of object reference
322651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         types, and <tt>Arrays.deepEquals(e1, e2) would return true</tt>
322751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    <li> <tt>e1</tt> and <tt>e2</tt> are arrays of the same primitive
322851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         type, and the appropriate overloading of
322951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>Arrays.equals(e1, e2)</tt> would return true.
323051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    <li> <tt>e1 == e2</tt>
323151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    <li> <tt>e1.equals(e2)</tt> would return true.
323251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </ul>
323351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Note that this definition permits <tt>null</tt> elements at any depth.
323451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
323551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If either of the specified arrays contain themselves as elements
323651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * either directly or indirectly through one or more levels of arrays,
323751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the behavior of this method is undefined.
323851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
323951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a1 one array to be tested for equality
324051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
324151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
324251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #equals(Object[],Object[])
324351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see Objects#deepEquals(Object, Object)
324451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
324551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
324651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean deepEquals(Object[] a1, Object[] a2) {
324751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a1 == a2)
324851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
324951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a1 == null || a2==null)
325051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
325151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a1.length;
325251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
325351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
325451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
325551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; i < length; i++) {
325651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object e1 = a1[i];
325751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object e2 = a2[i];
325851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
325951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (e1 == e2)
326051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                continue;
3261669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            if (e1 == null || e2 == null)
326251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
326351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
326451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            // Figure out whether the two elements are equal
326551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            boolean eq = deepEquals0(e1, e2);
326651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
326751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (!eq)
326851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
326951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
327051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
327151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
327251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
327351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    static boolean deepEquals0(Object e1, Object e2) {
3274669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        Class<?> cl1 = e1.getClass().getComponentType();
3275669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        Class<?> cl2 = e2.getClass().getComponentType();
3276669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak
3277669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        if (cl1 != cl2) {
3278669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return false;
3279669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        }
3280669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        if (e1 instanceof Object[])
3281669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return deepEquals ((Object[]) e1, (Object[]) e2);
3282669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == byte.class)
3283669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((byte[]) e1, (byte[]) e2);
3284669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == short.class)
3285669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((short[]) e1, (short[]) e2);
3286669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == int.class)
3287669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((int[]) e1, (int[]) e2);
3288669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == long.class)
3289669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((long[]) e1, (long[]) e2);
3290669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == char.class)
3291669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((char[]) e1, (char[]) e2);
3292669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == float.class)
3293669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((float[]) e1, (float[]) e2);
3294669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == double.class)
3295669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((double[]) e1, (double[]) e2);
3296669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == boolean.class)
3297669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((boolean[]) e1, (boolean[]) e2);
329851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
3299669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return e1.equals(e2);
330051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
330151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
330251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
330351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
330451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
330551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
330651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
330751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
330851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(long)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
330951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
331051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
331151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
331251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
331351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
331451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
331551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(long[] a) {
331651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
331751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
331851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
331951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
332051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
332151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
332251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
332351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
332451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
332551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
332651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
332751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
332851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
332951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
333051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
333151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
333251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
333351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
333451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
333551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
333651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
333751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
333851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(int)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt> is
333951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>null</tt>.
334051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
334151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
334251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
334351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
334451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
334551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(int[] a) {
334651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
334751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
334851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
334951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
335051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
335151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
335251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
335351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
335451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
335551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
335651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
335751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
335851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
335951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
336051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
336151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
336251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
336351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
336451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
336551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
336651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
336751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
336851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(short)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
336951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
337051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
337151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
337251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
337351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
337451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
337551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(short[] a) {
337651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
337751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
337851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
337951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
338051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
338151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
338251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
338351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
338451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
338551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
338651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
338751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
338851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
338951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
339051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
339151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
339251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
339351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
339451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
339551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
339651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
339751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
339851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(char)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
339951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
340051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
340151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
340251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
340351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
340451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
340551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(char[] a) {
340651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
340751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
340851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
340951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
341051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
341151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
341251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
341351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
341451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
341551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
341651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
341751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
341851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
341951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
342051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
342151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
342251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
342351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
342451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
342551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements
342651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are separated by the characters <tt>", "</tt> (a comma followed
342751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by a space).  Elements are converted to strings as by
342851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(byte)</tt>.  Returns <tt>"null"</tt> if
342951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>a</tt> is <tt>null</tt>.
343051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
343151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
343251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
343351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
343451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
343551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(byte[] a) {
343651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
343751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
343851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
343951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
344051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
344151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
344251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
344351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
344451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
344551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
344651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
344751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
344851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
344951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
345051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
345151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
345251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
345351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
345451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
345551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
345651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
345751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
345851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(boolean)</tt>.  Returns <tt>"null"</tt> if
345951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>a</tt> is <tt>null</tt>.
346051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
346151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
346251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
346351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
346451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
346551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(boolean[] a) {
346651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
346751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
346851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
346951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
347051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
347151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
347251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
347351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
347451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
347551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
347651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
347751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
347851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
347951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
348051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
348151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
348251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
348351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
348451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
348551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
348651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
348751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
348851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(float)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
348951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
349051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
349151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
349251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
349351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
349451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
349551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(float[] a) {
349651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
349751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
349851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
349951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
350051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
350151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
350251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
350351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
350451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
350551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
350651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
350751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
350851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
350951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
351051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
351151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
351251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
351351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
351451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
351551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
351651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
351751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
351851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
351951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(double)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
352051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
352151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
352251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
352351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
352451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
352551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
352651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(double[] a) {
352751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
352851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
352951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
353051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
353151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
353251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
353351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
353451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
353551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
353651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
353751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
353851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
353951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
354051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
354151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
354251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
354351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
354451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
354551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the array contains other arrays as elements, they are converted to
354651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * strings by the {@link Object#toString} method inherited from
354751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Object</tt>, which describes their <i>identities</i> rather than
354851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * their contents.
354951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
355051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is equal to the value that would
355151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * be returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt>
355251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>, in which case <tt>"null"</tt> is returned.
355351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
355451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
355551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
355651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #deepToString(Object[])
355751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
355851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
355951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(Object[] a) {
356051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
356151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
356251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
356351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
356451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
356551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
356651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
356751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
356851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
356951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
357051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(String.valueOf(a[i]));
357151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
357251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
357351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
357451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
357551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
357651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
357751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
357851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the "deep contents" of the specified
357951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array.  If the array contains other arrays as elements, the string
358051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * representation contains their contents and so on.  This method is
358151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * designed for converting multidimensional arrays to strings.
358251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
358351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The string representation consists of a list of the array's
358451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent
358551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements are separated by the characters <tt>", "</tt> (a comma
358651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * followed by a space).  Elements are converted to strings as by
358751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(Object)</tt>, unless they are themselves
358851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
358951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
359051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If an element <tt>e</tt> is an array of a primitive type, it is
359151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * converted to a string as by invoking the appropriate overloading of
359251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.toString(e)</tt>.  If an element <tt>e</tt> is an array of a
359351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * reference type, it is converted to a string as by invoking
359451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this method recursively.
359551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
359651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>To avoid infinite recursion, if the specified array contains itself
359751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * as an element, or contains an indirect reference to itself through one
359851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * or more levels of arrays, the self-reference is converted to the string
359951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>"[...]"</tt>.  For example, an array containing only a reference
360051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to itself would be rendered as <tt>"[[...]]"</tt>.
360151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
360251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This method returns <tt>"null"</tt> if the specified array
360351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
360451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
360551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
360651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
360751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #toString(Object[])
360851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
360951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
361051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String deepToString(Object[] a) {
361151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
361251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
361351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
361451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int bufLen = 20 * a.length;
361551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a.length != 0 && bufLen <= 0)
361651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            bufLen = Integer.MAX_VALUE;
361751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder buf = new StringBuilder(bufLen);
361851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        deepToString(a, buf, new HashSet<Object[]>());
361951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return buf.toString();
362051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
362151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
362251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void deepToString(Object[] a, StringBuilder buf,
362351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     Set<Object[]> dejaVu) {
362451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null) {
362551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            buf.append("null");
362651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
362751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
362851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
362951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1) {
363051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            buf.append("[]");
363151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
363251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
363351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
363451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        dejaVu.add(a);
363551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        buf.append('[');
363651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
363751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
363851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object element = a[i];
363951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (element == null) {
364051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                buf.append("null");
364151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            } else {
364251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                Class eClass = element.getClass();
364351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
364451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                if (eClass.isArray()) {
364551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    if (eClass == byte[].class)
364651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((byte[]) element));
364751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == short[].class)
364851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((short[]) element));
364951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == int[].class)
365051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((int[]) element));
365151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == long[].class)
365251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((long[]) element));
365351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == char[].class)
365451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((char[]) element));
365551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == float[].class)
365651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((float[]) element));
365751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == double[].class)
365851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((double[]) element));
365951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == boolean[].class)
366051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((boolean[]) element));
366151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else { // element is an array of object references
366251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        if (dejaVu.contains(element))
366351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                            buf.append("[...]");
366451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        else
366551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                            deepToString((Object[])element, buf, dejaVu);
366651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    }
366751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                } else {  // element is non-null and not an array
366851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    buf.append(element.toString());
366951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                }
367051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
367151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
367251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                break;
367351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            buf.append(", ");
367451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
367551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        buf.append(']');
367651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        dejaVu.remove(a);
367751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
3678983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski
3679983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski    /**
3680983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski     * Checks that the range described by {@code offset} and {@code count} doesn't exceed
3681983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski     * {@code arrayLength}.
3682983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski     *
3683983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski     * Android changed.
3684983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski     * @hide
3685983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski     */
3686983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski    public static void checkOffsetAndCount(int arrayLength, int offset, int count) {
3687983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski        if ((offset | count) < 0 || offset > arrayLength || arrayLength - offset < count) {
3688983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski            throw new ArrayIndexOutOfBoundsException(arrayLength, offset,
3689983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski                    count);
3690983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski        }
3691983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski    }
369251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski}
3693