151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/*
22c87ad3a45cecf9e344487cad1abfdebe79f2c7cNarayan Kamath * Copyright (C) 2014 The Android Open Source Project
31eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it
751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as
851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation.  Oracle designates this
951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided
1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code.
1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT
1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that
1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code).
1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version
1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation,
2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any
2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions.
2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.util;
2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
29839c3929d114963f4da459267bdd29b4837c290eTobias Thiererimport java.lang.reflect.Array;
30471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniakimport java.util.concurrent.ForkJoinPool;
311eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kongimport java.util.function.BinaryOperator;
32e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmeraimport java.util.function.Consumer;
331eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kongimport java.util.function.DoubleBinaryOperator;
341eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kongimport java.util.function.IntBinaryOperator;
35e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmeraimport java.util.function.IntFunction;
36e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmeraimport java.util.function.IntToDoubleFunction;
37e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmeraimport java.util.function.IntToLongFunction;
38e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmeraimport java.util.function.IntUnaryOperator;
391eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kongimport java.util.function.LongBinaryOperator;
402c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmeraimport java.util.function.UnaryOperator;
41ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkinimport java.util.stream.DoubleStream;
42ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkinimport java.util.stream.IntStream;
43ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkinimport java.util.stream.LongStream;
44ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkinimport java.util.stream.Stream;
45ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkinimport java.util.stream.StreamSupport;
4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/**
4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This class contains various methods for manipulating arrays (such as
4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sorting and searching). This class also contains a static factory
5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that allows arrays to be viewed as lists.
5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The methods in this class all throw a {@code NullPointerException},
5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the specified array reference is null, except where noted.
5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The documentation for the methods contained in this class includes
5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * briefs description of the <i>implementations</i>. Such descriptions should
5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be regarded as <i>implementation notes</i>, rather than parts of the
5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>specification</i>. Implementors should feel free to substitute other
5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * algorithms, so long as the specification itself is adhered to. (For
6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * example, the algorithm used by {@code sort(Object[])} does not have to be
6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a MergeSort, but it does have to be <i>stable</i>.)
6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This class is a member of the
64d2449bb576ad1e3a3877364e5e1ae28625f69e35Yi Kong * <a href="{@docRoot}openjdk-redirect.html?v=8&path=/technotes/guides/collections/index.html">
6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Java Collections Framework</a>.
6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Josh Bloch
6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Neal Gafter
6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author John Rose
7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since  1.2
7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic class Arrays {
7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
74471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
75471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * The minimum array length below which a parallel sorting
76471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * algorithm will not further partition the sorting task. Using
77471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * smaller sizes typically results in memory contention across
78471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * tasks that makes parallel speedups unlikely.
79471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @hide
80471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
811d70b849467dfcfa30f495e6d03bcbdef47b2588Tobias Thierer    // Android-changed: make public (used by harmony ArraysTest)
82471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static final int MIN_ARRAY_SORT_GRAN = 1 << 13;
83471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
84839c3929d114963f4da459267bdd29b4837c290eTobias Thierer    // Suppresses default constructor, ensuring non-instantiability.
85839c3929d114963f4da459267bdd29b4837c290eTobias Thierer    private Arrays() {}
86471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
87471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
88471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * A comparator that implements the natural ordering of a group of
89471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * mutually comparable elements. May be used when a supplied
90471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * comparator is null. To simplify code-sharing within underlying
91471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * implementations, the compare method only declares type Object
92471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * for its second argument.
93471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
94471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays class implementor's note: It is an empirical matter
95471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * whether ComparableTimSort offers any performance benefit over
96471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * TimSort used with this comparator.  If not, you are better off
97471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * deleting or bypassing ComparableTimSort.  There is currently no
98471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * empirical case for separating them for parallel sorting, so all
99471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * public Object parallelSort methods use the same comparator
100471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * based implementation.
101471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
102471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    static final class NaturalOrder implements Comparator<Object> {
103471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        @SuppressWarnings("unchecked")
104471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        public int compare(Object first, Object second) {
105471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            return ((Comparable<Object>)first).compareTo(second);
106471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        }
107471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        static final NaturalOrder INSTANCE = new NaturalOrder();
108471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
109471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
110839c3929d114963f4da459267bdd29b4837c290eTobias Thierer    /**
111839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * Checks that {@code fromIndex} and {@code toIndex} are in
1121d70b849467dfcfa30f495e6d03bcbdef47b2588Tobias Thierer     * the range and throws an exception if they aren't.
113839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     */
1141d70b849467dfcfa30f495e6d03bcbdef47b2588Tobias Thierer    private static void rangeCheck(int arrayLength, int fromIndex, int toIndex) {
115839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        if (fromIndex > toIndex) {
116839c3929d114963f4da459267bdd29b4837c290eTobias Thierer            throw new IllegalArgumentException(
1171d70b849467dfcfa30f495e6d03bcbdef47b2588Tobias Thierer                    "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
118839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        }
119839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        if (fromIndex < 0) {
120839c3929d114963f4da459267bdd29b4837c290eTobias Thierer            throw new ArrayIndexOutOfBoundsException(fromIndex);
121839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        }
1221d70b849467dfcfa30f495e6d03bcbdef47b2588Tobias Thierer        if (toIndex > arrayLength) {
123839c3929d114963f4da459267bdd29b4837c290eTobias Thierer            throw new ArrayIndexOutOfBoundsException(toIndex);
124839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        }
125839c3929d114963f4da459267bdd29b4837c290eTobias Thierer    }
126839c3929d114963f4da459267bdd29b4837c290eTobias Thierer
1279eca269fb2932fb9fd99b6e898600e7a21ade0e0Tobias Thierer    // BEGIN Android-added: checkOffsetAndCount() helper method for AIOOBE enforcement.
128839c3929d114963f4da459267bdd29b4837c290eTobias Thierer    /**
129839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * Checks that the range described by {@code offset} and {@code count} doesn't exceed
130839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * {@code arrayLength}.
131839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * @hide
132839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     */
133839c3929d114963f4da459267bdd29b4837c290eTobias Thierer    public static void checkOffsetAndCount(int arrayLength, int offset, int count) {
134839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        if ((offset | count) < 0 || offset > arrayLength || arrayLength - offset < count) {
135839c3929d114963f4da459267bdd29b4837c290eTobias Thierer            throw new ArrayIndexOutOfBoundsException(arrayLength, offset,
136839c3929d114963f4da459267bdd29b4837c290eTobias Thierer                    count);
137839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        }
138839c3929d114963f4da459267bdd29b4837c290eTobias Thierer    }
1399eca269fb2932fb9fd99b6e898600e7a21ade0e0Tobias Thierer    // END Android-added: checkOffsetAndCount() helper method for AIOOBE enforcement.
14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /*
142839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * Sorting methods. Note that all public "sort" methods take the
143839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * same form: Performing argument checks if necessary, and then
144839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * expanding arguments into those required for the internal
145839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * implementation methods residing in other package-private
146839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * classes (except for legacyMergeSort, included in this class).
14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
14851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
14951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
15051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(int[] a) {
161c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
16451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
17151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
17251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
17351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
17451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
17551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
17751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
17851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
17951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
18051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
18151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
18251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
18351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
18451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(int[] a, int fromIndex, int toIndex) {
18551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
186c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
18751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
18851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
18951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
19051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
19151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
19451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
19751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
19851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
19951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
20051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(long[] a) {
201c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
22351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(long[] a, int fromIndex, int toIndex) {
22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
226c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
23851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(short[] a) {
241c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
24451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
24551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
24651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
24751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
24851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
24951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
25051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
26151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
26251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(short[] a, int fromIndex, int toIndex) {
26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
266c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
26751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(char[] a) {
281c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(char[] a, int fromIndex, int toIndex) {
30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
306c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
31951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(byte[] a) {
321c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1);
32251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
32351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
34251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(byte[] a, int fromIndex, int toIndex) {
34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The {@code <} relation does not provide a total order on all float
35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * value compares neither less than, greater than, nor equal to any value,
35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * even itself. This method uses the total order imposed by the method
35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * other value and all {@code Float.NaN} values are considered equal.
35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(float[] a) {
369c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The {@code <} relation does not provide a total order on all float
37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * value compares neither less than, greater than, nor equal to any value,
38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * even itself. This method uses the total order imposed by the method
38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * other value and all {@code Float.NaN} values are considered equal.
38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(float[] a, int fromIndex, int toIndex) {
40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
402c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
40451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
40851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The {@code <} relation does not provide a total order on all double
40951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
41051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * value compares neither less than, greater than, nor equal to any value,
41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * even itself. This method uses the total order imposed by the method
41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * other value and all {@code Double.NaN} values are considered equal.
41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
42251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
42351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(double[] a) {
425c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
42851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The {@code <} relation does not provide a total order on all double
43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * value compares neither less than, greater than, nor equal to any value,
43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * even itself. This method uses the total order imposed by the method
43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * other value and all {@code Double.NaN} values are considered equal.
44151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
44451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
44551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
44651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
44751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
44851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
44951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
45051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
45151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
45251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
45351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
45451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
45551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
45651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(double[] a, int fromIndex, int toIndex) {
45751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
458c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
45951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
461471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
462471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified array into ascending numerical order.
463471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
464471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
465471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
466471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
467471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort}
468471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
469471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
470471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a
471471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * working space no greater than the size of the original array. The
472471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
473471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * execute any parallel tasks.
474471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
475471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
476471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
477471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
478471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
479471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(byte[] a) {
480471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = a.length, p, g;
481471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
482471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
483471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, 0, n - 1);
484471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
485471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJByte.Sorter
486471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new byte[n], 0, n, 0,
487471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
488471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
489471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
490471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
491471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
492471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified range of the array into ascending numerical order.
493471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * The range to be sorted extends from the index {@code fromIndex},
494471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * inclusive, to the index {@code toIndex}, exclusive. If
495471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code fromIndex == toIndex}, the range to be sorted is empty.
496471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
497471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
498471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
499471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
500471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort}
501471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
502471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
503471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a working
504471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * space no greater than the size of the specified range of the original
505471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
506471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * used to execute any parallel tasks.
507471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
508471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
509471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param fromIndex the index of the first element, inclusive, to be sorted
510471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param toIndex the index of the last element, exclusive, to be sorted
511471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
512471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
513471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws ArrayIndexOutOfBoundsException
514471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
515471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
516471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
517471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
518471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(byte[] a, int fromIndex, int toIndex) {
519471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        rangeCheck(a.length, fromIndex, toIndex);
520471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = toIndex - fromIndex, p, g;
521471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
522471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
523471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
524471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
525471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJByte.Sorter
526471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new byte[n], fromIndex, n, 0,
527471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
528471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
529471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
530471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
531471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
532471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified array into ascending numerical order.
533471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
534471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
535471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
536471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
537471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort}
538471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
539471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
540471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a
541471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * working space no greater than the size of the original array. The
542471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
543471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * execute any parallel tasks.
544471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
545471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
546471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
547471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
548471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
549471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(char[] a) {
550471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = a.length, p, g;
551471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
552471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
553471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
554471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
555471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJChar.Sorter
556471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new char[n], 0, n, 0,
557471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
558471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
559471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
560471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
561471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
562471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified range of the array into ascending numerical order.
563471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * The range to be sorted extends from the index {@code fromIndex},
564471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * inclusive, to the index {@code toIndex}, exclusive. If
565471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code fromIndex == toIndex}, the range to be sorted is empty.
566471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
567471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak      @implNote The sorting algorithm is a parallel sort-merge that breaks the
568471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
569471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
570471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort}
571471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
572471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
573471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a working
574471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * space no greater than the size of the specified range of the original
575471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
576471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * used to execute any parallel tasks.
577471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
578471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
579471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param fromIndex the index of the first element, inclusive, to be sorted
580471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param toIndex the index of the last element, exclusive, to be sorted
581471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
582471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
583471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws ArrayIndexOutOfBoundsException
584471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
585471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
586471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
587471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
588471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(char[] a, int fromIndex, int toIndex) {
589471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        rangeCheck(a.length, fromIndex, toIndex);
590471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = toIndex - fromIndex, p, g;
591471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
592471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
593471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
594471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
595471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJChar.Sorter
596471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new char[n], fromIndex, n, 0,
597471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
598471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
599471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
600471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
601471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
602471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified array into ascending numerical order.
603471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
604471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
605471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
606471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
607471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort}
608471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
609471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
610471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a
611471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * working space no greater than the size of the original array. The
612471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
613471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * execute any parallel tasks.
614471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
615471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
616471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
617471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
618471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
619471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(short[] a) {
620471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = a.length, p, g;
621471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
622471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
623471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
624471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
625471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJShort.Sorter
626471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new short[n], 0, n, 0,
627471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
628471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
629471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
630471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
631471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
632471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified range of the array into ascending numerical order.
633471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * The range to be sorted extends from the index {@code fromIndex},
634471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * inclusive, to the index {@code toIndex}, exclusive. If
635471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code fromIndex == toIndex}, the range to be sorted is empty.
636471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
637471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
638471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
639471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
640471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort}
641471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
642471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
643471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a working
644471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * space no greater than the size of the specified range of the original
645471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
646471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * used to execute any parallel tasks.
647471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
648471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
649471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param fromIndex the index of the first element, inclusive, to be sorted
650471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param toIndex the index of the last element, exclusive, to be sorted
651471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
652471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
653471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws ArrayIndexOutOfBoundsException
654471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
655471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
656471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
657471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
658471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(short[] a, int fromIndex, int toIndex) {
659471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        rangeCheck(a.length, fromIndex, toIndex);
660471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = toIndex - fromIndex, p, g;
661471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
662471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
663471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
664471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
665471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJShort.Sorter
666471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new short[n], fromIndex, n, 0,
667471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
668471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
669471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
670471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
671471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
672471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified array into ascending numerical order.
673471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
674471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
675471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
676471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
677471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort}
678471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
679471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
680471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a
681471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * working space no greater than the size of the original array. The
682471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
683471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * execute any parallel tasks.
684471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
685471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
686471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
687471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
688471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
689471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(int[] a) {
690471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = a.length, p, g;
691471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
692471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
693471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
694471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
695471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJInt.Sorter
696471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new int[n], 0, n, 0,
697471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
698471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
699471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
700471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
701471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
702471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified range of the array into ascending numerical order.
703471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * The range to be sorted extends from the index {@code fromIndex},
704471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * inclusive, to the index {@code toIndex}, exclusive. If
705471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code fromIndex == toIndex}, the range to be sorted is empty.
706471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
707471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
708471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
709471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
710471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort}
711471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
712471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
713471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a working
714471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * space no greater than the size of the specified range of the original
715471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
716471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * used to execute any parallel tasks.
717471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
718471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
719471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param fromIndex the index of the first element, inclusive, to be sorted
720471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param toIndex the index of the last element, exclusive, to be sorted
721471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
722471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
723471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws ArrayIndexOutOfBoundsException
724471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
725471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
726471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
727471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
728471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(int[] a, int fromIndex, int toIndex) {
729471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        rangeCheck(a.length, fromIndex, toIndex);
730471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = toIndex - fromIndex, p, g;
731471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
732471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
733471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
734471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
735471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJInt.Sorter
736471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new int[n], fromIndex, n, 0,
737471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
738471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
739471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
740471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
741471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
742471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified array into ascending numerical order.
743471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
744471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
745471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
746471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
747471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort}
748471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
749471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
750471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a
751471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * working space no greater than the size of the original array. The
752471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
753471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * execute any parallel tasks.
754471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
755471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
756471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
757471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
758471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
759471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(long[] a) {
760471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = a.length, p, g;
761471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
762471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
763471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
764471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
765471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJLong.Sorter
766471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new long[n], 0, n, 0,
767471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
768471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
769471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
770471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
771471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
772471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified range of the array into ascending numerical order.
773471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * The range to be sorted extends from the index {@code fromIndex},
774471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * inclusive, to the index {@code toIndex}, exclusive. If
775471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code fromIndex == toIndex}, the range to be sorted is empty.
776471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
777471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
778471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
779471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
780471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort}
781471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
782471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
783471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a working
784471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * space no greater than the size of the specified range of the original
785471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
786471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * used to execute any parallel tasks.
787471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
788471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
789471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param fromIndex the index of the first element, inclusive, to be sorted
790471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param toIndex the index of the last element, exclusive, to be sorted
791471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
792471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
793471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws ArrayIndexOutOfBoundsException
794471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
795471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
796471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
797471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
798471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(long[] a, int fromIndex, int toIndex) {
799471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        rangeCheck(a.length, fromIndex, toIndex);
800471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = toIndex - fromIndex, p, g;
801471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
802471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
803471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
804471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
805471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJLong.Sorter
806471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new long[n], fromIndex, n, 0,
807471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
808471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
809471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
810471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
811471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
812471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified array into ascending numerical order.
813471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
814471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * <p>The {@code <} relation does not provide a total order on all float
815471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
816471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * value compares neither less than, greater than, nor equal to any value,
817471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * even itself. This method uses the total order imposed by the method
818471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
819471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
820471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * other value and all {@code Float.NaN} values are considered equal.
821471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
822471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
823471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
824471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
825471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort}
826471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
827471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
828471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a
829471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * working space no greater than the size of the original array. The
830471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
831471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * execute any parallel tasks.
832471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
833471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
834471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
835471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
836471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
837471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(float[] a) {
838471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = a.length, p, g;
839471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
840471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
841471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
842471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
843471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJFloat.Sorter
844471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new float[n], 0, n, 0,
845471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
846471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
847471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
848471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
849471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
850471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified range of the array into ascending numerical order.
851471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * The range to be sorted extends from the index {@code fromIndex},
852471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * inclusive, to the index {@code toIndex}, exclusive. If
853471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code fromIndex == toIndex}, the range to be sorted is empty.
854471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
855471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * <p>The {@code <} relation does not provide a total order on all float
856471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
857471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * value compares neither less than, greater than, nor equal to any value,
858471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * even itself. This method uses the total order imposed by the method
859471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
860471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
861471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * other value and all {@code Float.NaN} values are considered equal.
862471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
863471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
864471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
865471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
866471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort}
867471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
868471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
869471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a working
870471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * space no greater than the size of the specified range of the original
871471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
872471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * used to execute any parallel tasks.
873471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
874471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
875471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param fromIndex the index of the first element, inclusive, to be sorted
876471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param toIndex the index of the last element, exclusive, to be sorted
877471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
878471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
879471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws ArrayIndexOutOfBoundsException
880471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
881471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
882471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
883471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
884471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(float[] a, int fromIndex, int toIndex) {
885471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        rangeCheck(a.length, fromIndex, toIndex);
886471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = toIndex - fromIndex, p, g;
887471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
888471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
889471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
890471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
891471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJFloat.Sorter
892471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new float[n], fromIndex, n, 0,
893471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
894471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
895471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
896471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
897471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
898471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified array into ascending numerical order.
899471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
900471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * <p>The {@code <} relation does not provide a total order on all double
901471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
902471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * value compares neither less than, greater than, nor equal to any value,
903471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * even itself. This method uses the total order imposed by the method
904471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
905471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
906471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * other value and all {@code Double.NaN} values are considered equal.
907471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
908471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
909471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
910471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
911471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort}
912471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
913471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
914471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a
915471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * working space no greater than the size of the original array. The
916471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
917471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * execute any parallel tasks.
918471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
919471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
920471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
921471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
922471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
923471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(double[] a) {
924471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = a.length, p, g;
925471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
926471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
927471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
928471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
929471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJDouble.Sorter
930471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new double[n], 0, n, 0,
931471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
932471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
933471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
934471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
935471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
936471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified range of the array into ascending numerical order.
937471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * The range to be sorted extends from the index {@code fromIndex},
938471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * inclusive, to the index {@code toIndex}, exclusive. If
939471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code fromIndex == toIndex}, the range to be sorted is empty.
940471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
941471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * <p>The {@code <} relation does not provide a total order on all double
942471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
943471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * value compares neither less than, greater than, nor equal to any value,
944471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * even itself. This method uses the total order imposed by the method
945471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
946471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
947471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * other value and all {@code Double.NaN} values are considered equal.
948471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
949471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
950471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
951471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
952471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort}
953471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
954471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
955471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a working
956471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * space no greater than the size of the specified range of the original
957471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
958471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * used to execute any parallel tasks.
959471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
960471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
961471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param fromIndex the index of the first element, inclusive, to be sorted
962471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param toIndex the index of the last element, exclusive, to be sorted
963471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
964471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
965471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws ArrayIndexOutOfBoundsException
966471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
967471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
968471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
969471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
970471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static void parallelSort(double[] a, int fromIndex, int toIndex) {
971471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        rangeCheck(a.length, fromIndex, toIndex);
972471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = toIndex - fromIndex, p, g;
973471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
974471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
975471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
976471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
977471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJDouble.Sorter
978471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a, new double[n], fromIndex, n, 0,
979471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
980471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g).invoke();
981471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
982471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
983471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
984471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified array of objects into ascending order, according
985471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * to the {@linkplain Comparable natural ordering} of its elements.
986471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * All elements in the array must implement the {@link Comparable}
987471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * interface.  Furthermore, all elements in the array must be
988471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
989471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * not throw a {@code ClassCastException} for any elements {@code e1}
990471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * and {@code e2} in the array).
991471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
992471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
993471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * not be reordered as a result of the sort.
994471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
995471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
996471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
997471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
998471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
999471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
1000471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
1001471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
1002471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * working space no greater than the size of the original array. The
1003471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
1004471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * execute any parallel tasks.
1005471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1006471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param <T> the class of the objects to be sorted
1007471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
1008471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1009471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws ClassCastException if the array contains elements that are not
1010471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         <i>mutually comparable</i> (for example, strings and integers)
1011471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws IllegalArgumentException (optional) if the natural
1012471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         ordering of the array elements is found to violate the
1013471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         {@link Comparable} contract
1014471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1015471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
1016471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
1017471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    @SuppressWarnings("unchecked")
1018471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static <T extends Comparable<? super T>> void parallelSort(T[] a) {
1019471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = a.length, p, g;
1020471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
1021471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
1022471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            TimSort.sort(a, 0, n, NaturalOrder.INSTANCE, null, 0, 0);
1023471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
1024471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJObject.Sorter<T>
1025471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a,
1026471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
1027471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
1028471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
1029471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
1030471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
1031471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
1032471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified range of the specified array of objects into
1033471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * ascending order, according to the
1034471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@linkplain Comparable natural ordering} of its
1035471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * elements.  The range to be sorted extends from index
1036471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
1037471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * (If {@code fromIndex==toIndex}, the range to be sorted is empty.)  All
1038471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * elements in this range must implement the {@link Comparable}
1039471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * interface.  Furthermore, all elements in this range must be <i>mutually
1040471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a
1041471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code ClassCastException} for any elements {@code e1} and
1042471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code e2} in the array).
1043471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1044471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
1045471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * not be reordered as a result of the sort.
1046471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1047471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
1048471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
1049471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
1050471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
1051471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
1052471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
1053471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
1054471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * space no greater than the size of the specified range of the original
1055471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
1056471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * used to execute any parallel tasks.
1057471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1058471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param <T> the class of the objects to be sorted
1059471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
1060471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param fromIndex the index of the first element (inclusive) to be
1061471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *        sorted
1062471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param toIndex the index of the last element (exclusive) to be sorted
1063471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
1064471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         (optional) if the natural ordering of the array elements is
1065471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         found to violate the {@link Comparable} contract
1066471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
1067471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         {@code toIndex > a.length}
1068471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws ClassCastException if the array contains elements that are
1069471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         not <i>mutually comparable</i> (for example, strings and
1070471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         integers).
1071471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1072471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
1073471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
1074471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    @SuppressWarnings("unchecked")
1075471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static <T extends Comparable<? super T>>
1076471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    void parallelSort(T[] a, int fromIndex, int toIndex) {
1077471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        rangeCheck(a.length, fromIndex, toIndex);
1078471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = toIndex - fromIndex, p, g;
1079471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
1080471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
1081471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            TimSort.sort(a, fromIndex, toIndex, NaturalOrder.INSTANCE, null, 0, 0);
1082471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
1083471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJObject.Sorter<T>
1084471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a,
1085471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
1086471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
1087471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
1088471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
1089471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
1090471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
1091471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified array of objects according to the order induced by
1092471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the specified comparator.  All elements in the array must be
1093471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * <i>mutually comparable</i> by the specified comparator (that is,
1094471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
1095471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * for any elements {@code e1} and {@code e2} in the array).
1096471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1097471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
1098471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * not be reordered as a result of the sort.
1099471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1100471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
1101471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
1102471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
1103471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
1104471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
1105471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
1106471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
1107471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * working space no greater than the size of the original array. The
1108471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
1109471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * execute any parallel tasks.
1110471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1111471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param <T> the class of the objects to be sorted
1112471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
1113471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param cmp the comparator to determine the order of the array.  A
1114471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *        {@code null} value indicates that the elements'
1115471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *        {@linkplain Comparable natural ordering} should be used.
1116471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws ClassCastException if the array contains elements that are
1117471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         not <i>mutually comparable</i> using the specified comparator
1118471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws IllegalArgumentException (optional) if the comparator is
1119471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         found to violate the {@link java.util.Comparator} contract
1120471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1121471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
1122471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
1123471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    @SuppressWarnings("unchecked")
1124471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) {
1125471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (cmp == null)
1126471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            cmp = NaturalOrder.INSTANCE;
1127471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = a.length, p, g;
1128471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
1129471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
1130471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            TimSort.sort(a, 0, n, cmp, null, 0, 0);
1131471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
1132471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJObject.Sorter<T>
1133471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a,
1134471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
1135471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
1136471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g, cmp).invoke();
1137471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
1138471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
1139471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    /**
1140471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Sorts the specified range of the specified array of objects according
1141471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * to the order induced by the specified comparator.  The range to be
1142471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted extends from index {@code fromIndex}, inclusive, to index
1143471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
1144471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * range to be sorted is empty.)  All elements in the range must be
1145471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * <i>mutually comparable</i> by the specified comparator (that is,
1146471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
1147471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * for any elements {@code e1} and {@code e2} in the range).
1148471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1149471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
1150471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * not be reordered as a result of the sort.
1151471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1152471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
1153471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array into sub-arrays that are themselves sorted and then merged. When
1154471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * the sub-array length reaches a minimum granularity, the sub-array is
1155471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
1156471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * method. If the length of the specified array is less than the minimum
1157471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * granularity, then it is sorted using the appropriate {@link
1158471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
1159471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * space no greater than the size of the specified range of the original
1160471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
1161471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * used to execute any parallel tasks.
1162471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1163471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param <T> the class of the objects to be sorted
1164471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param a the array to be sorted
1165471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param fromIndex the index of the first element (inclusive) to be
1166471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *        sorted
1167471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param toIndex the index of the last element (exclusive) to be sorted
1168471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @param cmp the comparator to determine the order of the array.  A
1169471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *        {@code null} value indicates that the elements'
1170471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *        {@linkplain Comparable natural ordering} should be used.
1171471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
1172471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         (optional) if the natural ordering of the array elements is
1173471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         found to violate the {@link Comparable} contract
1174471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
1175471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         {@code toIndex > a.length}
1176471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @throws ClassCastException if the array contains elements that are
1177471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         not <i>mutually comparable</i> (for example, strings and
1178471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *         integers).
1179471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     *
1180471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     * @since 1.8
1181471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak     */
1182471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    @SuppressWarnings("unchecked")
1183471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    public static <T> void parallelSort(T[] a, int fromIndex, int toIndex,
1184471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                                        Comparator<? super T> cmp) {
1185471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        rangeCheck(a.length, fromIndex, toIndex);
1186471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (cmp == null)
1187471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            cmp = NaturalOrder.INSTANCE;
1188471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        int n = toIndex - fromIndex, p, g;
1189471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        if (n <= MIN_ARRAY_SORT_GRAN ||
1190471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
1191471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            TimSort.sort(a, fromIndex, toIndex, cmp, null, 0, 0);
1192471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak        else
1193471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak            new ArraysParallelSortHelpers.FJObject.Sorter<T>
1194471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                (null, a,
1195471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
1196471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
1197471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak                 MIN_ARRAY_SORT_GRAN : g, cmp).invoke();
1198471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak    }
1199471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak
120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /*
120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting of complex type arrays.
120251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array of objects into ascending order, according
120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to the {@linkplain Comparable natural ordering} of its elements.
120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * All elements in the array must implement the {@link Comparable}
120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interface.  Furthermore, all elements in the array must be
120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not throw a {@code ClassCastException} for any elements {@code e1}
121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and {@code e2} in the array).
121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
121451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not be reordered as a result of the sort.
121551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
121651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: This implementation is a stable, adaptive,
121751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * iterative mergesort that requires far fewer than n lg(n) comparisons
121851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * when the input array is partially sorted, while offering the
121951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * performance of a traditional mergesort when the input array is
122051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * randomly ordered.  If the input array is nearly sorted, the
122151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation requires approximately n comparisons.  Temporary
122251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * storage requirements vary from a small constant for nearly sorted
122351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input arrays to n/2 object references for randomly ordered input
122451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
122551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
122651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation takes equal advantage of ascending and
122751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * descending order in its input array, and can take advantage of
122851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending and descending order in different parts of the the same
122951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input array.  It is well-suited to merging two or more sorted arrays:
123051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * simply concatenate the arrays and sort the resulting array.
123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
123251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation was adapted from Tim Peters's list sort for Python
123351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
1234839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic
123551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting and Information Theoretic Complexity", in Proceedings of the
123651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
123751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * January 1993.
123851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
123951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
124051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are not
124151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>mutually comparable</i> (for example, strings and integers)
124251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException (optional) if the natural
124351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         ordering of the array elements is found to violate the
124451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         {@link Comparable} contract
124551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
124651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(Object[] a) {
1247c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer        // Android-changed: LegacyMergeSort is no longer supported
1248c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer        // if (LegacyMergeSort.userRequested)
1249c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer        //     legacyMergeSort(a);
1250c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer        // else
1251c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            ComparableTimSort.sort(a, 0, a.length, null, 0, 0);
125251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
125351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
125451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
125551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the specified array of objects into
125651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending order, according to the
125751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@linkplain Comparable natural ordering} of its
125851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements.  The range to be sorted extends from index
125951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
126051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (If {@code fromIndex==toIndex}, the range to be sorted is empty.)  All
126151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements in this range must implement the {@link Comparable}
126251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interface.  Furthermore, all elements in this range must be <i>mutually
126351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a
126451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code ClassCastException} for any elements {@code e1} and
126551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code e2} in the array).
126651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
126751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
126851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not be reordered as a result of the sort.
126951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
127051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: This implementation is a stable, adaptive,
127151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * iterative mergesort that requires far fewer than n lg(n) comparisons
127251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * when the input array is partially sorted, while offering the
127351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * performance of a traditional mergesort when the input array is
127451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * randomly ordered.  If the input array is nearly sorted, the
127551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation requires approximately n comparisons.  Temporary
127651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * storage requirements vary from a small constant for nearly sorted
127751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input arrays to n/2 object references for randomly ordered input
127851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
127951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
128051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation takes equal advantage of ascending and
128151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * descending order in its input array, and can take advantage of
128251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending and descending order in different parts of the the same
128351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input array.  It is well-suited to merging two or more sorted arrays:
128451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * simply concatenate the arrays and sort the resulting array.
128551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
128651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation was adapted from Tim Peters's list sort for Python
128751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
1288839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic
128951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting and Information Theoretic Complexity", in Proceedings of the
129051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
129151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * January 1993.
129251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
129351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
129451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
129551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        sorted
129651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be sorted
129751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
129851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         (optional) if the natural ordering of the array elements is
129951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         found to violate the {@link Comparable} contract
130051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
130151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         {@code toIndex > a.length}
130251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are
130351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         not <i>mutually comparable</i> (for example, strings and
130451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         integers).
130551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
130651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(Object[] a, int fromIndex, int toIndex) {
1307c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        rangeCheck(a.length, fromIndex, toIndex);
1308c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer        // Android-changed: LegacyMergeSort is no longer supported
1309c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer        // if (LegacyMergeSort.userRequested)
1310c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer        //     legacyMergeSort(a, fromIndex, toIndex);
1311c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer        // else
1312c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            ComparableTimSort.sort(a, fromIndex, toIndex, null, 0, 0);
131351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
131451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
131551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
131651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Tuning parameter: list size at or below which insertion sort will be
131751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * used in preference to mergesort.
131851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * To be removed in a future release.
131951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
132051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static final int INSERTIONSORT_THRESHOLD = 7;
132151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
132251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
132351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Src is the source array that starts at index 0
132451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Dest is the (possibly larger) array destination with a possible offset
132551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * low is the index in dest to start sorting
132651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * high is the end index in dest to end sorting
132751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * off is the offset to generate corresponding low, high in src
132851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * To be removed in a future release.
132951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
1330839c3929d114963f4da459267bdd29b4837c290eTobias Thierer    @SuppressWarnings({"unchecked", "rawtypes"})
133151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void mergeSort(Object[] src,
133251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  Object[] dest,
133351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  int low,
133451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  int high,
133551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  int off) {
133651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = high - low;
133751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
133851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Insertion sort on smallest arrays
133951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (length < INSERTIONSORT_THRESHOLD) {
134051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            for (int i=low; i<high; i++)
134151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                for (int j=i; j>low &&
134251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
134351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    swap(dest, j, j-1);
134451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
134551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
134651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
134751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Recursively sort halves of dest into src
134851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int destLow  = low;
134951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int destHigh = high;
135051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        low  += off;
135151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        high += off;
135251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int mid = (low + high) >>> 1;
135351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(dest, src, low, mid, -off);
135451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(dest, src, mid, high, -off);
135551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
135651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // If list is already sorted, just copy from src to dest.  This is an
135751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // optimization that results in faster sorts for nearly ordered lists.
135851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
135951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            System.arraycopy(src, low, dest, destLow, length);
136051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
136151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
136251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
136351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Merge sorted halves (now in src) into dest
136451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
136551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
136651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                dest[i] = src[p++];
136751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
136851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                dest[i] = src[q++];
136951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
137051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
137151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
137251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
137351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Swaps x[a] with x[b].
137451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
137551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void swap(Object[] x, int a, int b) {
137651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        Object t = x[a];
137751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        x[a] = x[b];
137851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        x[b] = t;
137951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
138051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
138151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
138251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array of objects according to the order induced by
138351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified comparator.  All elements in the array must be
138451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>mutually comparable</i> by the specified comparator (that is,
138551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
138651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * for any elements {@code e1} and {@code e2} in the array).
138751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
138851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
138951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not be reordered as a result of the sort.
139051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
139151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: This implementation is a stable, adaptive,
139251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * iterative mergesort that requires far fewer than n lg(n) comparisons
139351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * when the input array is partially sorted, while offering the
139451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * performance of a traditional mergesort when the input array is
139551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * randomly ordered.  If the input array is nearly sorted, the
139651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation requires approximately n comparisons.  Temporary
139751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * storage requirements vary from a small constant for nearly sorted
139851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input arrays to n/2 object references for randomly ordered input
139951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
140051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
140151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation takes equal advantage of ascending and
140251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * descending order in its input array, and can take advantage of
140351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending and descending order in different parts of the the same
140451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input array.  It is well-suited to merging two or more sorted arrays:
140551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * simply concatenate the arrays and sort the resulting array.
140651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
140751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation was adapted from Tim Peters's list sort for Python
140851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
1409839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic
141051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting and Information Theoretic Complexity", in Proceedings of the
141151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
141251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * January 1993.
141351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
1414839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * @param <T> the class of the objects to be sorted
141551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
141651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param c the comparator to determine the order of the array.  A
141751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@code null} value indicates that the elements'
141851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@linkplain Comparable natural ordering} should be used.
141951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are
142051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         not <i>mutually comparable</i> using the specified comparator
142151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException (optional) if the comparator is
142251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         found to violate the {@link Comparator} contract
142351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
142451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> void sort(T[] a, Comparator<? super T> c) {
1425c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        if (c == null) {
1426c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            sort(a);
1427c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        } else {
1428c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer            // Android-changed: LegacyMergeSort is no longer supported
1429c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer            // if (LegacyMergeSort.userRequested)
1430c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer            //     legacyMergeSort(a, c);
1431c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer            // else
1432c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath                TimSort.sort(a, 0, a.length, c, null, 0, 0);
1433c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        }
143451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
143551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
143651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
143751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the specified array of objects according
143851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to the order induced by the specified comparator.  The range to be
143951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * sorted extends from index {@code fromIndex}, inclusive, to index
144051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
144151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be sorted is empty.)  All elements in the range must be
144251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>mutually comparable</i> by the specified comparator (that is,
144351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
144451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * for any elements {@code e1} and {@code e2} in the range).
144551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
144651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
144751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not be reordered as a result of the sort.
144851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
144951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: This implementation is a stable, adaptive,
145051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * iterative mergesort that requires far fewer than n lg(n) comparisons
145151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * when the input array is partially sorted, while offering the
145251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * performance of a traditional mergesort when the input array is
145351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * randomly ordered.  If the input array is nearly sorted, the
145451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation requires approximately n comparisons.  Temporary
145551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * storage requirements vary from a small constant for nearly sorted
145651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input arrays to n/2 object references for randomly ordered input
145751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
145851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
145951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation takes equal advantage of ascending and
146051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * descending order in its input array, and can take advantage of
146151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending and descending order in different parts of the the same
146251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input array.  It is well-suited to merging two or more sorted arrays:
146351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * simply concatenate the arrays and sort the resulting array.
146451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
146551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation was adapted from Tim Peters's list sort for Python
146651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
1467839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic
146851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting and Information Theoretic Complexity", in Proceedings of the
146951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
147051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * January 1993.
147151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
1472839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * @param <T> the class of the objects to be sorted
147351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
147451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
147551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        sorted
147651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be sorted
147751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param c the comparator to determine the order of the array.  A
147851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@code null} value indicates that the elements'
147951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@linkplain Comparable natural ordering} should be used.
148051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are not
148151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>mutually comparable</i> using the specified comparator.
148251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
148351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         (optional) if the comparator is found to violate the
148451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         {@link Comparator} contract
148551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
148651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         {@code toIndex > a.length}
148751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
148851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> void sort(T[] a, int fromIndex, int toIndex,
148951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                Comparator<? super T> c) {
1490c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        if (c == null) {
1491c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            sort(a, fromIndex, toIndex);
1492c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        } else {
1493c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            rangeCheck(a.length, fromIndex, toIndex);
1494c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer            // Android-changed: LegacyMergeSort is no longer supported
1495c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer            // if (LegacyMergeSort.userRequested)
1496c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer            //     legacyMergeSort(a, fromIndex, toIndex, c);
1497c6493c449b5e3e9d295d0504b3148370f1ec8de0Tobias Thierer            // else
1498c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath                TimSort.sort(a, fromIndex, toIndex, c, null, 0, 0);
1499c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        }
150051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
150151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
15021eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    // Parallel prefix
15031eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong
15041eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    /**
15051eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * Cumulates, in parallel, each element of the given array in place,
15061eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * using the supplied function. For example if the array initially
15071eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * holds {@code [2, 1, 0, 3]} and the operation performs addition,
15081eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * then upon return the array holds {@code [2, 3, 3, 6]}.
15091eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * Parallel prefix computation is usually more efficient than
15101eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * sequential loops for large arrays.
15111eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     *
15121eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param <T> the class of the objects in the array
15131eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param array the array, which is modified in-place by this method
15141eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param op a side-effect-free, associative function to perform the
15151eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * cumulation
15161eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws NullPointerException if the specified array or function is null
15171eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @since 1.8
15181eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     */
15191eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op) {
15201eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        Objects.requireNonNull(op);
15211eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        if (array.length > 0)
15221eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong            new ArrayPrefixHelpers.CumulateTask<>
15231eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong                    (null, op, array, 0, array.length).invoke();
15241eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    }
15251eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong
15261eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    /**
15271eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * Performs {@link #parallelPrefix(Object[], BinaryOperator)}
15281eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * for the given subrange of the array.
15291eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     *
15301eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param <T> the class of the objects in the array
15311eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param array the array
15321eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param fromIndex the index of the first element, inclusive
15331eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param toIndex the index of the last element, exclusive
15341eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param op a side-effect-free, associative function to perform the
15351eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * cumulation
15361eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
15371eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws ArrayIndexOutOfBoundsException
15381eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
15391eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws NullPointerException if the specified array or function is null
15401eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @since 1.8
15411eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     */
15421eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    public static <T> void parallelPrefix(T[] array, int fromIndex,
15431eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong                                          int toIndex, BinaryOperator<T> op) {
15441eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        Objects.requireNonNull(op);
15451eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        rangeCheck(array.length, fromIndex, toIndex);
15461eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        if (fromIndex < toIndex)
15471eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong            new ArrayPrefixHelpers.CumulateTask<>
15481eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong                    (null, op, array, fromIndex, toIndex).invoke();
15491eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    }
15501eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong
15511eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    /**
15521eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * Cumulates, in parallel, each element of the given array in place,
15531eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * using the supplied function. For example if the array initially
15541eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * holds {@code [2, 1, 0, 3]} and the operation performs addition,
15551eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * then upon return the array holds {@code [2, 3, 3, 6]}.
15561eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * Parallel prefix computation is usually more efficient than
15571eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * sequential loops for large arrays.
15581eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     *
15591eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param array the array, which is modified in-place by this method
15601eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param op a side-effect-free, associative function to perform the
15611eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * cumulation
15621eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws NullPointerException if the specified array or function is null
15631eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @since 1.8
15641eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     */
15651eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    public static void parallelPrefix(long[] array, LongBinaryOperator op) {
15661eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        Objects.requireNonNull(op);
15671eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        if (array.length > 0)
15681eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong            new ArrayPrefixHelpers.LongCumulateTask
15691eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong                    (null, op, array, 0, array.length).invoke();
15701eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    }
15711eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong
15721eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    /**
15731eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * Performs {@link #parallelPrefix(long[], LongBinaryOperator)}
15741eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * for the given subrange of the array.
15751eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     *
15761eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param array the array
15771eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param fromIndex the index of the first element, inclusive
15781eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param toIndex the index of the last element, exclusive
15791eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param op a side-effect-free, associative function to perform the
15801eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * cumulation
15811eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
15821eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws ArrayIndexOutOfBoundsException
15831eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
15841eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws NullPointerException if the specified array or function is null
15851eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @since 1.8
15861eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     */
15871eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    public static void parallelPrefix(long[] array, int fromIndex,
15881eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong                                      int toIndex, LongBinaryOperator op) {
15891eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        Objects.requireNonNull(op);
15901eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        rangeCheck(array.length, fromIndex, toIndex);
15911eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        if (fromIndex < toIndex)
15921eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong            new ArrayPrefixHelpers.LongCumulateTask
15931eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong                    (null, op, array, fromIndex, toIndex).invoke();
15941eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    }
15951eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong
15961eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    /**
15971eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * Cumulates, in parallel, each element of the given array in place,
15981eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * using the supplied function. For example if the array initially
15991eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * holds {@code [2.0, 1.0, 0.0, 3.0]} and the operation performs addition,
16001eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * then upon return the array holds {@code [2.0, 3.0, 3.0, 6.0]}.
16011eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * Parallel prefix computation is usually more efficient than
16021eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * sequential loops for large arrays.
16031eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     *
16041eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * <p> Because floating-point operations may not be strictly associative,
16051eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * the returned result may not be identical to the value that would be
16061eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * obtained if the operation was performed sequentially.
16071eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     *
16081eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param array the array, which is modified in-place by this method
16091eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param op a side-effect-free function to perform the cumulation
16101eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws NullPointerException if the specified array or function is null
16111eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @since 1.8
16121eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     */
16131eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    public static void parallelPrefix(double[] array, DoubleBinaryOperator op) {
16141eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        Objects.requireNonNull(op);
16151eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        if (array.length > 0)
16161eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong            new ArrayPrefixHelpers.DoubleCumulateTask
16171eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong                    (null, op, array, 0, array.length).invoke();
16181eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    }
16191eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong
16201eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    /**
16211eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * Performs {@link #parallelPrefix(double[], DoubleBinaryOperator)}
16221eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * for the given subrange of the array.
16231eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     *
16241eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param array the array
16251eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param fromIndex the index of the first element, inclusive
16261eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param toIndex the index of the last element, exclusive
16271eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param op a side-effect-free, associative function to perform the
16281eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * cumulation
16291eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
16301eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws ArrayIndexOutOfBoundsException
16311eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
16321eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws NullPointerException if the specified array or function is null
16331eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @since 1.8
16341eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     */
16351eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    public static void parallelPrefix(double[] array, int fromIndex,
16361eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong                                      int toIndex, DoubleBinaryOperator op) {
16371eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        Objects.requireNonNull(op);
16381eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        rangeCheck(array.length, fromIndex, toIndex);
16391eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        if (fromIndex < toIndex)
16401eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong            new ArrayPrefixHelpers.DoubleCumulateTask
16411eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong                    (null, op, array, fromIndex, toIndex).invoke();
16421eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    }
16431eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong
16441eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    /**
16451eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * Cumulates, in parallel, each element of the given array in place,
16461eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * using the supplied function. For example if the array initially
16471eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * holds {@code [2, 1, 0, 3]} and the operation performs addition,
16481eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * then upon return the array holds {@code [2, 3, 3, 6]}.
16491eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * Parallel prefix computation is usually more efficient than
16501eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * sequential loops for large arrays.
16511eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     *
16521eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param array the array, which is modified in-place by this method
16531eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param op a side-effect-free, associative function to perform the
16541eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * cumulation
16551eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws NullPointerException if the specified array or function is null
16561eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @since 1.8
16571eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     */
16581eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    public static void parallelPrefix(int[] array, IntBinaryOperator op) {
16591eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        Objects.requireNonNull(op);
16601eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        if (array.length > 0)
16611eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong            new ArrayPrefixHelpers.IntCumulateTask
16621eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong                    (null, op, array, 0, array.length).invoke();
16631eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    }
16641eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong
16651eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    /**
16661eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * Performs {@link #parallelPrefix(int[], IntBinaryOperator)}
16671eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * for the given subrange of the array.
16681eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     *
16691eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param array the array
16701eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param fromIndex the index of the first element, inclusive
16711eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param toIndex the index of the last element, exclusive
16721eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @param op a side-effect-free, associative function to perform the
16731eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * cumulation
16741eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
16751eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws ArrayIndexOutOfBoundsException
16761eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
16771eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @throws NullPointerException if the specified array or function is null
16781eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     * @since 1.8
16791eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong     */
16801eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    public static void parallelPrefix(int[] array, int fromIndex,
16811eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong                                      int toIndex, IntBinaryOperator op) {
16821eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        Objects.requireNonNull(op);
16831eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        rangeCheck(array.length, fromIndex, toIndex);
16841eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong        if (fromIndex < toIndex)
16851eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong            new ArrayPrefixHelpers.IntCumulateTask
16861eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong                    (null, op, array, fromIndex, toIndex).invoke();
16871eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong    }
16881eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong
168951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Searching
169051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
169151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
169251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of longs for the specified value using the
169351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.  The array must be sorted (as
169451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(long[])} method) prior to making this call.  If it
169551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the array contains
169651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
169751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
169851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
169951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
170051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
170151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
170251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
170351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
170451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
170551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
170651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
170751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
170851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
170951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
171051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(long[] a, long key) {
171151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
171251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
171351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
171451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
171551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
171651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of longs for the specified value using the
171751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.
171851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted (as
171951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(long[], int, int)} method)
172051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If it
172151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the range contains
172251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
172351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
172451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
172551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
172651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
172751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
172851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
172951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
173051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
173151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
173251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
173351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
173451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
173551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
173651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
173751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
173851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
173951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
174051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
174151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
174251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
174351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
174451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
174551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
174651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(long[] a, int fromIndex, int toIndex,
174751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   long key) {
174851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
174951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
175051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
175151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
175251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
175351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(long[] a, int fromIndex, int toIndex,
175451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     long key) {
175551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
175651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
175751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
175851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
175951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
176051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            long midVal = a[mid];
176151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
176251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
176351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
176451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
176551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
176651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
176751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
176851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
176951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
177051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
177151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
177251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
177351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of ints for the specified value using the
177451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.  The array must be sorted (as
177551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(int[])} method) prior to making this call.  If it
177651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the array contains
177751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
177851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
177951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
178051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
178151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
178251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
178351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
178451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
178551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
178651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
178751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
178851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
178951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
179051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
179151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(int[] a, int key) {
179251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
179351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
179451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
179551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
179651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
179751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of ints for the specified value using the
179851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.
179951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted (as
180051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(int[], int, int)} method)
180151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If it
180251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the range contains
180351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
180451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
180551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
180651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
180751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
180851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
180951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
181051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
181151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
181251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
181351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
181451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
181551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
181651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
181751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
181851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
181951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
182051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
182151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
182251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
182351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
182451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
182551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
182651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
182751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(int[] a, int fromIndex, int toIndex,
182851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   int key) {
182951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
183051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
183151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
183251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
183351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
183451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(int[] a, int fromIndex, int toIndex,
183551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     int key) {
183651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
183751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
183851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
183951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
184051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
184151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int midVal = a[mid];
184251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
184351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
184451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
184551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
184651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
184751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
184851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
184951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
185051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
185151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
185251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
185351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
185451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of shorts for the specified value using
185551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.  The array must be sorted
185651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(short[])} method) prior to making this call.  If
185751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * it is not sorted, the results are undefined.  If the array contains
185851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
185951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
186051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
186151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
186251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
186351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
186451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
186551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
186651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
186751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
186851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
186951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
187051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
187151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
187251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(short[] a, short key) {
187351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
187451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
187551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
187651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
187751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
187851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of shorts for the specified value using
187951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.
188051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted
188151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(short[], int, int)} method)
188251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If
188351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * it is not sorted, the results are undefined.  If the range contains
188451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
188551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
188651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
188751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
188851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
188951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
189051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
189151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
189251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
189351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
189451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
189551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
189651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
189751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
189851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
189951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
190051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
190151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
190251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
190351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
190451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
190551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
190651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
190751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
190851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(short[] a, int fromIndex, int toIndex,
190951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   short key) {
191051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
191151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
191251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
191351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
191451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
191551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(short[] a, int fromIndex, int toIndex,
191651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     short key) {
191751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
191851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
191951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
192051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
192151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
192251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            short midVal = a[mid];
192351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
192451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
192551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
192651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
192751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
192851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
192951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
193051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
193151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
193251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
193351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
193451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
193551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of chars for the specified value using the
193651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.  The array must be sorted (as
193751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(char[])} method) prior to making this call.  If it
193851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the array contains
193951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
194051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
194151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
194251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
194351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
194451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
194551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
194651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
194751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
194851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
194951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
195051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
195151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
195251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
195351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(char[] a, char key) {
195451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
195551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
195651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
195751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
195851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
195951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of chars for the specified value using the
196051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.
196151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted (as
196251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(char[], int, int)} method)
196351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If it
196451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the range contains
196551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
196651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
196751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
196851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
196951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
197051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
197151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
197251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
197351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
197451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
197551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
197651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
197751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
197851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
197951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
198051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
198151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
198251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
198351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
198451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
198551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
198651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
198751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
198851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
198951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(char[] a, int fromIndex, int toIndex,
199051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   char key) {
199151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
199251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
199351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
199451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
199551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
199651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(char[] a, int fromIndex, int toIndex,
199751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     char key) {
199851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
199951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
200051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
200151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
200251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
200351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            char midVal = a[mid];
200451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
200551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
200651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
200751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
200851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
200951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
201051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
201151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
201251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
201351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
201451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
201551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
201651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of bytes for the specified value using the
201751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.  The array must be sorted (as
201851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(byte[])} method) prior to making this call.  If it
201951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the array contains
202051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
202151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
202251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
202351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
202451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
202551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
202651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
202751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
202851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
202951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
203051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
203151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
203251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
203351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
203451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(byte[] a, byte key) {
203551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
203651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
203751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
203851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
203951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
204051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of bytes for the specified value using the
204151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.
204251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted (as
204351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(byte[], int, int)} method)
204451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If it
204551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the range contains
204651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
204751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
204851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
204951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
205051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
205151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
205251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
205351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
205451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
205551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
205651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
205751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
205851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
205951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
206051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
206151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
206251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
206351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
206451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
206551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
206651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
206751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
206851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
206951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
207051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(byte[] a, int fromIndex, int toIndex,
207151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   byte key) {
207251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
207351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
207451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
207551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
207651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
207751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
207851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     byte key) {
207951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
208051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
208151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
208251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
208351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
208451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            byte midVal = a[mid];
208551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
208651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
208751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
208851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
208951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
209051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
209151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
209251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
209351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
209451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
209551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
209651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
209751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of doubles for the specified value using
209851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.  The array must be sorted
209951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(double[])} method) prior to making this call.
210051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If it is not sorted, the results are undefined.  If the array contains
210151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
210251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.  This method considers all NaN values to be
210351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equivalent and equal.
210451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
210551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
210651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
210751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
210851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
210951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
211051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
211151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
211251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
211351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
211451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
211551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
211651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(double[] a, double key) {
211751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
211851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
211951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
212051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
212151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
212251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of doubles for the specified value using
212351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.
212451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted
212551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(double[], int, int)} method)
212651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.
212751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If it is not sorted, the results are undefined.  If the range contains
212851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
212951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.  This method considers all NaN values to be
213051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equivalent and equal.
213151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
213251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
213351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
213451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
213551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
213651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
213751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
213851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
213951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
214051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
214151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
214251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
214351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
214451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
214551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
214651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
214751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
214851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
214951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
215051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
215151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
215251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
215351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(double[] a, int fromIndex, int toIndex,
215451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   double key) {
215551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
215651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
215751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
215851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
215951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
216051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(double[] a, int fromIndex, int toIndex,
216151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     double key) {
216251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
216351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
216451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
216551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
216651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
216751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            double midVal = a[mid];
216851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
216951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
217051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;  // Neither val is NaN, thisVal is smaller
217151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
217251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1; // Neither val is NaN, thisVal is larger
217351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else {
217451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                long midBits = Double.doubleToLongBits(midVal);
217551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                long keyBits = Double.doubleToLongBits(key);
217651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                if (midBits == keyBits)     // Values are equal
217751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    return mid;             // Key found
217851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
217951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    low = mid + 1;
218051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                else                        // (0.0, -0.0) or (NaN, !NaN)
218151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    high = mid - 1;
218251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
218351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
218451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
218551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
218651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
218751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
218851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of floats for the specified value using
218951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm. The array must be sorted
219051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(float[])} method) prior to making this call. If
219151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * it is not sorted, the results are undefined. If the array contains
219251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
219351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found. This method considers all NaN values to be
219451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equivalent and equal.
219551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
219651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
219751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
219851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
219951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
220051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
220151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
220251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
220351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key. Note
220451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
220551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
220651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
220751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(float[] a, float key) {
220851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
220951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
221051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
221151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
221251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
221351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of floats for the specified value using
221451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.
221551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted
221651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(float[], int, int)} method)
221751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call. If
221851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * it is not sorted, the results are undefined. If the range contains
221951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
222051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found. This method considers all NaN values to be
222151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equivalent and equal.
222251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
222351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
222451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
222551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
222651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
222751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
222851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
222951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
223051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
223151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
223251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
223351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
223451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
223551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key. Note
223651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
223751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
223851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
223951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
224051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
224151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
224251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
224351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
224451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(float[] a, int fromIndex, int toIndex,
224551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   float key) {
224651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
224751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
224851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
224951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
225051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
225151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(float[] a, int fromIndex, int toIndex,
225251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     float key) {
225351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
225451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
225551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
225651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
225751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
225851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            float midVal = a[mid];
225951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
226051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
226151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;  // Neither val is NaN, thisVal is smaller
226251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
226351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1; // Neither val is NaN, thisVal is larger
226451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else {
226551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                int midBits = Float.floatToIntBits(midVal);
226651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                int keyBits = Float.floatToIntBits(key);
226751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                if (midBits == keyBits)     // Values are equal
226851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    return mid;             // Key found
226951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
227051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    low = mid + 1;
227151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                else                        // (0.0, -0.0) or (NaN, !NaN)
227251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    high = mid - 1;
227351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
227451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
227551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
227651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
227751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
227851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
227951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array for the specified object using the binary
228051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * search algorithm. The array must be sorted into ascending order
228151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * according to the
228251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@linkplain Comparable natural ordering}
228351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of its elements (as by the
228451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link #sort(Object[])} method) prior to making this call.
228551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If it is not sorted, the results are undefined.
228651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (If the array contains elements that are not mutually comparable (for
228751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * example, strings and integers), it <i>cannot</i> be sorted according
228851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to the natural ordering of its elements, hence results are undefined.)
228951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the array contains multiple
229051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements equal to the specified object, there is no guarantee which
229151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
229251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
229351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
229451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
229551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
229651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
229751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
229851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
229951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
230051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
230151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
230251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
230351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the search key is not comparable to the
230451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements of the array.
230551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
230651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(Object[] a, Object key) {
230751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
230851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
230951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
231051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
231151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
231251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array for the specified object using the binary
231351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * search algorithm.
231451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted into ascending order
231551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * according to the
231651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@linkplain Comparable natural ordering}
231751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of its elements (as by the
231851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link #sort(Object[], int, int)} method) prior to making this
231951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * call.  If it is not sorted, the results are undefined.
232051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (If the range contains elements that are not mutually comparable (for
232151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * example, strings and integers), it <i>cannot</i> be sorted according
232251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to the natural ordering of its elements, hence results are undefined.)
232351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the range contains multiple
232451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements equal to the specified object, there is no guarantee which
232551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
232651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
232751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
232851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
232951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
233051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
233151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
233251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
233351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
233451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
233551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
233651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
233751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
233851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
233951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
234051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
234151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
234251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the search key is not comparable to the
234351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements of the array within the specified range.
234451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
234551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
234651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
234751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
234851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
234951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
235051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(Object[] a, int fromIndex, int toIndex,
235151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   Object key) {
235251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
235351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
235451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
235551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
235651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
235751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
235851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     Object key) {
235951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
236051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
236151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
236251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
236351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
2364839c3929d114963f4da459267bdd29b4837c290eTobias Thierer            @SuppressWarnings("rawtypes")
236551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Comparable midVal = (Comparable)a[mid];
2366839c3929d114963f4da459267bdd29b4837c290eTobias Thierer            @SuppressWarnings("unchecked")
236751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int cmp = midVal.compareTo(key);
236851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
236951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (cmp < 0)
237051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
237151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (cmp > 0)
237251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
237351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
237451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
237551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
237651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
237751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
237851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
237951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
238051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array for the specified object using the binary
238151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * search algorithm.  The array must be sorted into ascending order
238251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * according to the specified comparator (as by the
238351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link #sort(Object[], Comparator) sort(T[], Comparator)}
238451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method) prior to making this call.  If it is
238551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not sorted, the results are undefined.
238651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the array contains multiple
238751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements equal to the specified object, there is no guarantee which one
238851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * will be found.
238951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
2390839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * @param <T> the class of the objects in the array
239151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
239251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
239351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param c the comparator by which the array is ordered.  A
239451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        <tt>null</tt> value indicates that the elements'
239551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@linkplain Comparable natural ordering} should be used.
239651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
239751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
239851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
239951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
240051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
240151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
240251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
240351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
240451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are not
240551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>mutually comparable</i> using the specified comparator,
240651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or the search key is not comparable to the
240751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements of the array using this comparator.
240851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
240951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
241051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key, c);
241151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
241251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
241351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
241451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
241551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array for the specified object using the binary
241651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * search algorithm.
241751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted into ascending order
241851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * according to the specified comparator (as by the
241951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link #sort(Object[], int, int, Comparator)
242051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * sort(T[], int, int, Comparator)}
242151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method) prior to making this call.
242251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If it is not sorted, the results are undefined.
242351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the range contains multiple elements equal to the specified object,
242451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * there is no guarantee which one will be found.
242551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
2426839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * @param <T> the class of the objects in the array
242751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
242851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
242951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
243051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
243151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
243251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param c the comparator by which the array is ordered.  A
243351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        <tt>null</tt> value indicates that the elements'
243451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@linkplain Comparable natural ordering} should be used.
243551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
243651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
243751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
243851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
243951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
244051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
244151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
244251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
244351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
244451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
244551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the range contains elements that are not
244651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>mutually comparable</i> using the specified comparator,
244751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or the search key is not comparable to the
244851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range using this comparator.
244951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
245051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
245151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
245251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
245351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
245451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
245551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
245651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                       T key, Comparator<? super T> c) {
245751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
245851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key, c);
245951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
246051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
246151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
246251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
246351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                         T key, Comparator<? super T> c) {
246451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (c == null) {
246551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return binarySearch0(a, fromIndex, toIndex, key);
246651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
246751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
246851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
246951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
247051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
247151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
247251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            T midVal = a[mid];
247351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int cmp = c.compare(midVal, key);
247451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (cmp < 0)
247551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
247651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (cmp > 0)
247751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
247851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
247951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
248051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
248151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
248251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
248351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
248451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Equality Testing
248551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
248651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
248751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of longs are
248851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
248951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
249051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
249151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
249251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
249351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
249451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
249551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
249651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
249751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
249851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(long[] a, long[] a2) {
249951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
250051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
250151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
250251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
250351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
250451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
250551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
250651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
250751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
250851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
250951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
251051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
251151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
251251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
251351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
251451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
251551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
251651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of ints are
251751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
251851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
251951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
252051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
252151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
252251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
252351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
252451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
252551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
252651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
252751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(int[] a, int[] a2) {
252851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
252951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
253051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
253151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
253251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
253351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
253451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
253551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
253651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
253751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
253851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
253951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
254051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
254151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
254251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
254351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
254451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
254551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of shorts are
254651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
254751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
254851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
254951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
255051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
255151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
255251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
255351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
255451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
255551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
255651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(short[] a, short a2[]) {
255751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
255851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
255951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
256051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
256151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
256251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
256351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
256451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
256551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
256651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
256751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
256851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
256951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
257051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
257151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
257251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
257351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
257451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of chars are
257551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
257651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
257751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
257851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
257951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
258051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
258151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
258251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
258351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
258451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
258551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(char[] a, char[] a2) {
258651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
258751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
258851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
258951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
259051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
259151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
259251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
259351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
259451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
259551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
259651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
259751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
259851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
259951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
260051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
260151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
260251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
260351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of bytes are
260451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
260551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
260651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
260751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
260851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
260951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
261051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
261151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
261251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
261351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
261451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(byte[] a, byte[] a2) {
261551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
261651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
261751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
261851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
261951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
262051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
262151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
262251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
262351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
262451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
262551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
262651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
262751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
262851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
262951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
263051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
263151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
263251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of booleans are
263351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
263451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
263551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
263651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
263751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
263851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
263951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
264051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
264151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
264251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
264351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(boolean[] a, boolean[] a2) {
264451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
264551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
264651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
264751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
264851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
264951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
265051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
265151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
265251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
265351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
265451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
265551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
265651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
265751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
265851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
265951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
266051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
266151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of doubles are
266251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
266351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
266451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
266551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
266651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
266751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
266851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
266951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <pre>    <tt>new Double(d1).equals(new Double(d2))</tt></pre>
267051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (Unlike the <tt>==</tt> operator, this method considers
267151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
267251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
267351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
267451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
267551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
267651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see Double#equals(Object)
267751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
267851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(double[] a, double[] a2) {
267951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
268051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
268151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
268251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
268351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
268451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
268551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
268651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
268751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
268851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
268951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
269051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
269151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
269251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
269351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
269451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
269551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
269651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of floats are
269751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
269851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
269951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
270051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
270151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
270251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
270351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Two floats <tt>f1</tt> and <tt>f2</tt> are considered equal if:
270451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <pre>    <tt>new Float(f1).equals(new Float(f2))</tt></pre>
270551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (Unlike the <tt>==</tt> operator, this method considers
270651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>NaN</tt> equals to itself, and 0.0f unequal to -0.0f.)
270751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
270851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
270951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
271051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
271151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see Float#equals(Object)
271251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
271351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(float[] a, float[] a2) {
271451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
271551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
271651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
271751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
271851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
271951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
272051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
272151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
272251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
272351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
272451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
272551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
272651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
272751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
272851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
272951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
273051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
273151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of Objects are
273251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  The two arrays are considered equal if
273351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * both arrays contain the same number of elements, and all corresponding
273451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * pairs of elements in the two arrays are equal.  Two objects <tt>e1</tt>
273551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
273651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * : e1.equals(e2))</tt>.  In other words, the two arrays are equal if
273751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * they contain the same elements in the same order.  Also, two array
273851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * references are considered equal if both are <tt>null</tt>.<p>
273951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
274051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
274151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
274251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
274351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
274451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(Object[] a, Object[] a2) {
274551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
274651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
274751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
274851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
274951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
275051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
275151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
275251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
275351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
275451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++) {
275551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object o1 = a[i];
275651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object o2 = a2[i];
275751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (!(o1==null ? o2==null : o1.equals(o2)))
275851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
275951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
276051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
276151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
276251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
276351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
276451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Filling
276551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
276651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
276751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified long value to each element of the specified array
276851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of longs.
276951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
277051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
277151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
277251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
277351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(long[] a, long val) {
277451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
277551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
277651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
277751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
277851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
277951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified long value to each element of the specified
278051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of longs.  The range to be filled
278151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
278251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
278351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
278451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
278551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
278651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
278751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
278851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
278951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
279051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
279151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
279251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
279351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
279451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
279551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(long[] a, int fromIndex, int toIndex, long val) {
279651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
279751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
279851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
279951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
280051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
280151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
280251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified int value to each element of the specified array
280351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of ints.
280451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
280551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
280651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
280751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
280851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(int[] a, int val) {
280951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
281051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
281151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
281251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
281351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
281451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified int value to each element of the specified
281551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of ints.  The range to be filled
281651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
281751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
281851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
281951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
282051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
282151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
282251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
282351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
282451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
282551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
282651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
282751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
282851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
282951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
283051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(int[] a, int fromIndex, int toIndex, int val) {
283151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
283251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
283351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
283451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
283551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
283651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
283751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified short value to each element of the specified array
283851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of shorts.
283951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
284051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
284151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
284251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
284351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(short[] a, short val) {
284451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
284551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
284651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
284751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
284851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
284951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified short value to each element of the specified
285051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of shorts.  The range to be filled
285151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
285251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
285351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
285451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
285551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
285651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
285751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
285851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
285951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
286051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
286151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
286251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
286351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
286451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
286551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(short[] a, int fromIndex, int toIndex, short val) {
286651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
286751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
286851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
286951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
287051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
287151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
287251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified char value to each element of the specified array
287351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of chars.
287451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
287551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
287651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
287751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
287851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(char[] a, char val) {
287951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
288051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
288151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
288251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
288351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
288451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified char value to each element of the specified
288551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of chars.  The range to be filled
288651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
288751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
288851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
288951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
289051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
289151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
289251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
289351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
289451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
289551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
289651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
289751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
289851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
289951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
290051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(char[] a, int fromIndex, int toIndex, char val) {
290151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
290251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
290351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
290451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
290551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
290651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
290751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified byte value to each element of the specified array
290851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of bytes.
290951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
291051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
291151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
291251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
291351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(byte[] a, byte val) {
291451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
291551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
291651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
291751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
291851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
291951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified byte value to each element of the specified
292051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of bytes.  The range to be filled
292151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
292251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
292351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
292451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
292551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
292651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
292751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
292851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
292951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
293051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
293151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
293251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
293351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
293451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
293551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
293651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
293751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
293851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
293951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
294051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
294151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
294251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified boolean value to each element of the specified
294351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array of booleans.
294451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
294551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
294651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
294751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
294851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(boolean[] a, boolean val) {
294951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
295051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
295151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
295251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
295351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
295451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified boolean value to each element of the specified
295551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of booleans.  The range to be filled
295651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
295751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
295851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
295951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
296051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
296151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
296251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
296351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
296451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
296551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
296651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
296751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
296851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
296951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
297051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(boolean[] a, int fromIndex, int toIndex,
297151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                            boolean val) {
297251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
297351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
297451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
297551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
297651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
297751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
297851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified double value to each element of the specified
297951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array of doubles.
298051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
298151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
298251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
298351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
298451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(double[] a, double val) {
298551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
298651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
298751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
298851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
298951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
299051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified double value to each element of the specified
299151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of doubles.  The range to be filled
299251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
299351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
299451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
299551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
299651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
299751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
299851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
299951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
300051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
300151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
300251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
300351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
300451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
300551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
300651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(double[] a, int fromIndex, int toIndex,double val){
300751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
300851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
300951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
301051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
301151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
301251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
301351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified float value to each element of the specified array
301451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of floats.
301551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
301651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
301751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
301851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
301951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(float[] a, float val) {
302051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
302151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
302251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
302351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
302451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
302551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified float value to each element of the specified
302651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of floats.  The range to be filled
302751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
302851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
302951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
303051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
303151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
303251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
303351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
303451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
303551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
303651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
303751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
303851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
303951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
304051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
304151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(float[] a, int fromIndex, int toIndex, float val) {
304251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
304351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
304451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
304551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
304651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
304751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
304851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified Object reference to each element of the specified
304951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array of Objects.
305051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
305151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
305251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
305351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayStoreException if the specified value is not of a
305451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         runtime type that can be stored in the specified array
305551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
305651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(Object[] a, Object val) {
305751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
305851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
305951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
306051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
306151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
306251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified Object reference to each element of the specified
306351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of Objects.  The range to be filled
306451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
306551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
306651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
306751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
306851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
306951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
307051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
307151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
307251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
307351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
307451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
307551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
307651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
307751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayStoreException if the specified value is not of a
307851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         runtime type that can be stored in the specified array
307951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
308051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
308151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
308251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
308351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
308451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
308551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
308651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Cloning
308751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
308851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
308951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with nulls (if necessary)
309051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
309151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
309251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
309351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>null</tt>.
309451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
309551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
309651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The resulting array is of exactly the same class as the original array.
309751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
3098839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * @param <T> the class of the objects in the array
309951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
310051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
310151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with nulls
310251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
310351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
310451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
310551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
310651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
3107839c3929d114963f4da459267bdd29b4837c290eTobias Thierer    @SuppressWarnings("unchecked")
310851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> T[] copyOf(T[] original, int newLength) {
310951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return (T[]) copyOf(original, newLength, original.getClass());
311051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
311151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
311251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
311351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with nulls (if necessary)
311451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
311551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
311651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
311751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>null</tt>.
311851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
311951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
312051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The resulting array is of the class <tt>newType</tt>.
312151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
3122839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * @param <U> the class of the objects in the original array
3123839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * @param <T> the class of the objects in the returned array
312451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
312551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
312651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newType the class of the copy to be returned
312751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with nulls
312851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
312951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
313051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
313151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayStoreException if an element copied from
313251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     <tt>original</tt> is not of a runtime type that can be stored in
313351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     an array of class <tt>newType</tt>
313451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
313551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
313651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
3137839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        @SuppressWarnings("unchecked")
313851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        T[] copy = ((Object)newType == (Object)Object[].class)
313951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            ? (T[]) new Object[newLength]
314051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
314151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
314251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
314351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
314451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
314551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
314651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
314751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
314851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
314951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
315051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
315151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>(byte)0</tt>.
315251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
315351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
315451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
315551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
315651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
315751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
315851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
315951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
316051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
316151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
316251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
316351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static byte[] copyOf(byte[] original, int newLength) {
316451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        byte[] copy = new byte[newLength];
316551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
316651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
316751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
316851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
316951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
317051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
317151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
317251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
317351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
317451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
317551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>(short)0</tt>.
317651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
317751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
317851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
317951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
318051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
318151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
318251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
318351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
318451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
318551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
318651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
318751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static short[] copyOf(short[] original, int newLength) {
318851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        short[] copy = new short[newLength];
318951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
319051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
319151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
319251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
319351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
319451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
319551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
319651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
319751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
319851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
319951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>0</tt>.
320051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
320151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
320251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
320351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
320451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
320551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
320651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
320751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
320851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
320951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
321051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
321151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int[] copyOf(int[] original, int newLength) {
321251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int[] copy = new int[newLength];
321351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
321451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
321551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
321651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
321751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
321851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
321951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
322051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
322151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
322251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
322351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>0L</tt>.
322451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
322551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
322651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
322751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
322851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
322951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
323051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
323151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
323251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
323351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
323451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
323551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static long[] copyOf(long[] original, int newLength) {
323651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        long[] copy = new long[newLength];
323751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
323851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
323951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
324051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
324151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
324251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
324351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with null characters (if necessary)
324451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are valid
324551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * in both the original array and the copy, the two arrays will contain
324651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * identical values.  For any indices that are valid in the copy but not
324751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the original, the copy will contain <tt>'\\u000'</tt>.  Such indices
324851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * will exist if and only if the specified length is greater than that of
324951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the original array.
325051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
325151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
325251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
325351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with null characters
325451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
325551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
325651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
325751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
325851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
325951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static char[] copyOf(char[] original, int newLength) {
326051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        char[] copy = new char[newLength];
326151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
326251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
326351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
326451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
326551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
326651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
326751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
326851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
326951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
327051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
327151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>0f</tt>.
327251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
327351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
327451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
327551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
327651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
327751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
327851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
327951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
328051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
328151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
328251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
328351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static float[] copyOf(float[] original, int newLength) {
328451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        float[] copy = new float[newLength];
328551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
328651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
328751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
328851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
328951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
329051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
329151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
329251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
329351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
329451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
329551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>0d</tt>.
329651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
329751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
329851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
329951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
330051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
330151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
330251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
330351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
330451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
330551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
330651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
330751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static double[] copyOf(double[] original, int newLength) {
330851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        double[] copy = new double[newLength];
330951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
331051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
331151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
331251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
331351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
331451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
331551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with <tt>false</tt> (if necessary)
331651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
331751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
331851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
331951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>false</tt>.
332051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
332151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
332251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
332351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
332451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
332551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with false elements
332651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
332751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
332851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
332951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
333051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
333151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean[] copyOf(boolean[] original, int newLength) {
333251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        boolean[] copy = new boolean[newLength];
333351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
333451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
333551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
333651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
333751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
333851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
333951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
334051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
334151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
334251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
334351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
334451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
334551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
334651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
334751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
334851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>null</tt> is placed in all elements of the copy whose index is
334951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
335051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
335151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>
335251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The resulting array is of exactly the same class as the original array.
335351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
3354839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * @param <T> the class of the objects in the array
335551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
335651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
335751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
335851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
335951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
336051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with nulls to obtain the required length
336151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
336251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
336351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
336451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
336551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
336651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
3367839c3929d114963f4da459267bdd29b4837c290eTobias Thierer    @SuppressWarnings("unchecked")
336851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> T[] copyOfRange(T[] original, int from, int to) {
3369839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());
337051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
337151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
337251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
337351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
337451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
337551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
337651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
337751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
337851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
337951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
338051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
338151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
338251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>null</tt> is placed in all elements of the copy whose index is
338351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
338451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
338551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The resulting array is of the class <tt>newType</tt>.
338651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
3387839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * @param <U> the class of the objects in the original array
3388839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * @param <T> the class of the objects in the returned array
338951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
339051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
339151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
339251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
339351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newType the class of the copy to be returned
339451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
339551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with nulls to obtain the required length
339651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
339751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
339851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
339951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
340051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayStoreException if an element copied from
340151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     <tt>original</tt> is not of a runtime type that can be stored in
340251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     an array of class <tt>newType</tt>.
340351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
340451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
340551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
340651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
340751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
340851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
3409839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        @SuppressWarnings("unchecked")
341051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        T[] copy = ((Object)newType == (Object)Object[].class)
341151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            ? (T[]) new Object[newLength]
341251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
341351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
341451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
341551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
341651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
341751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
341851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
341951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
342051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
342151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
342251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
342351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
342451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
342551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
342651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
342751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
342851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>(byte)0</tt> is placed in all elements of the copy whose index is
342951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
343051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
343151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
343251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
343351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
343451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
343551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
343651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
343751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
343851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
343951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
344051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
344151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
344251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
344351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
344451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static byte[] copyOfRange(byte[] original, int from, int to) {
344551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
344651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
344751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
344851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        byte[] copy = new byte[newLength];
344951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
345051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
345151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
345251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
345351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
345451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
345551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
345651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
345751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
345851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
345951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
346051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
346151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
346251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
346351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
346451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>(short)0</tt> is placed in all elements of the copy whose index is
346551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
346651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
346751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
346851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
346951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
347051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
347151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
347251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
347351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
347451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
347551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
347651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
347751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
347851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
347951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
348051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static short[] copyOfRange(short[] original, int from, int to) {
348151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
348251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
348351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
348451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        short[] copy = new short[newLength];
348551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
348651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
348751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
348851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
348951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
349051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
349151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
349251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
349351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
349451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
349551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
349651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
349751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
349851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
349951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
350051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>0</tt> is placed in all elements of the copy whose index is
350151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
350251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
350351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
350451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
350551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
350651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
350751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
350851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
350951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
351051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
351151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
351251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
351351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
351451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
351551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
351651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int[] copyOfRange(int[] original, int from, int to) {
351751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
351851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
351951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
352051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int[] copy = new int[newLength];
352151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
352251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
352351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
352451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
352551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
352651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
352751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
352851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
352951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
353051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
353151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
353251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
353351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
353451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
353551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
353651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>0L</tt> is placed in all elements of the copy whose index is
353751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
353851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
353951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
354051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
354151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
354251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
354351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
354451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
354551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
354651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
354751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
354851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
354951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
355051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
355151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
355251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static long[] copyOfRange(long[] original, int from, int to) {
355351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
355451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
355551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
355651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        long[] copy = new long[newLength];
355751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
355851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
355951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
356051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
356151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
356251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
356351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
356451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
356551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
356651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
356751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
356851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
356951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
357051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
357151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
357251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>'\\u000'</tt> is placed in all elements of the copy whose index is
357351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
357451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
357551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
357651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
357751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
357851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
357951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
358051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
358151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with null characters to obtain the required length
358251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
358351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
358451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
358551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
358651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
358751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
358851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static char[] copyOfRange(char[] original, int from, int to) {
358951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
359051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
359151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
359251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        char[] copy = new char[newLength];
359351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
359451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
359551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
359651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
359751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
359851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
359951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
360051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
360151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
360251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
360351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
360451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
360551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
360651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
360751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
360851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>0f</tt> is placed in all elements of the copy whose index is
360951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
361051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
361151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
361251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
361351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
361451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
361551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
361651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
361751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
361851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
361951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
362051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
362151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
362251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
362351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
362451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static float[] copyOfRange(float[] original, int from, int to) {
362551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
362651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
362751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
362851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        float[] copy = new float[newLength];
362951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
363051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
363151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
363251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
363351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
363451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
363551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
363651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
363751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
363851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
363951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
364051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
364151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
364251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
364351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
364451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>0d</tt> is placed in all elements of the copy whose index is
364551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
364651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
364751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
364851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
364951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
365051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
365151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
365251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
365351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
365451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
365551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
365651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
365751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
365851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
365951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
366051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static double[] copyOfRange(double[] original, int from, int to) {
366151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
366251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
366351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
366451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        double[] copy = new double[newLength];
366551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
366651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
366751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
366851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
366951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
367051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
367151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
367251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
367351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
367451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
367551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
367651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
367751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
367851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
367951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
368051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>false</tt> is placed in all elements of the copy whose index is
368151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
368251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
368351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
368451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
368551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
368651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
368751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
368851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
368951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with false elements to obtain the required length
369051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
369151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
369251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
369351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
369451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
369551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
369651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean[] copyOfRange(boolean[] original, int from, int to) {
369751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
369851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
369951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
370051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        boolean[] copy = new boolean[newLength];
370151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
370251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
370351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
370451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
370551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
370651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Misc
370751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
370851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
370951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a fixed-size list backed by the specified array.  (Changes to
371051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the returned list "write through" to the array.)  This method acts
371151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * as bridge between array-based and collection-based APIs, in
371251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * combination with {@link Collection#toArray}.  The returned list is
371351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * serializable and implements {@link RandomAccess}.
371451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
371551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This method also provides a convenient way to create a fixed-size
371651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * list initialized to contain several elements:
371751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <pre>
371851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
371951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </pre>
372051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
3721839c3929d114963f4da459267bdd29b4837c290eTobias Thierer     * @param <T> the class of the objects in the array
372251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array by which the list will be backed
372351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a list view of the specified array
372451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
372551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @SafeVarargs
3726839c3929d114963f4da459267bdd29b4837c290eTobias Thierer    @SuppressWarnings("varargs")
372751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> List<T> asList(T... a) {
372851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return new ArrayList<>(a);
372951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
373051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
373151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
373251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @serial include
373351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
373451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static class ArrayList<E> extends AbstractList<E>
373551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        implements RandomAccess, java.io.Serializable
373651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    {
373751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        private static final long serialVersionUID = -2764017481108945198L;
373851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        private final E[] a;
373951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
374051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        ArrayList(E[] array) {
37414c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath            a = Objects.requireNonNull(array);
374251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
374351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
37444c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
374551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public int size() {
374651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return a.length;
374751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
374851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
37494c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
375051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public Object[] toArray() {
375151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return a.clone();
375251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
375351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
37544c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
37554c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @SuppressWarnings("unchecked")
375651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public <T> T[] toArray(T[] a) {
375751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int size = size();
375851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a.length < size)
375951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return Arrays.copyOf(this.a, size,
376051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     (Class<? extends T[]>) a.getClass());
376151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            System.arraycopy(this.a, 0, a, 0, size);
376251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a.length > size)
376351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                a[size] = null;
376451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return a;
376551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
376651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
37674c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
376851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public E get(int index) {
376951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return a[index];
377051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
377151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
37724c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
377351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public E set(int index, E element) {
377451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            E oldValue = a[index];
377551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[index] = element;
377651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return oldValue;
377751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
377851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
37794c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
378051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public int indexOf(Object o) {
3781839c3929d114963f4da459267bdd29b4837c290eTobias Thierer            E[] a = this.a;
3782839c3929d114963f4da459267bdd29b4837c290eTobias Thierer            if (o == null) {
3783839c3929d114963f4da459267bdd29b4837c290eTobias Thierer                for (int i = 0; i < a.length; i++)
3784839c3929d114963f4da459267bdd29b4837c290eTobias Thierer                    if (a[i] == null)
378551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        return i;
378651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            } else {
3787839c3929d114963f4da459267bdd29b4837c290eTobias Thierer                for (int i = 0; i < a.length; i++)
378851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    if (o.equals(a[i]))
378951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        return i;
379051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
379151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return -1;
379251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
379351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
37944c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
379551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public boolean contains(Object o) {
379651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return indexOf(o) != -1;
379751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
379802f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak
379902f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak        @Override
3800839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        public Spliterator<E> spliterator() {
3801839c3929d114963f4da459267bdd29b4837c290eTobias Thierer            return Spliterators.spliterator(a, Spliterator.ORDERED);
3802839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        }
3803839c3929d114963f4da459267bdd29b4837c290eTobias Thierer
3804839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        @Override
380502f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak        public void forEach(Consumer<? super E> action) {
380602f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak            Objects.requireNonNull(action);
380702f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak            for (E e : a) {
380802f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak                action.accept(e);
380902f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak            }
381002f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak        }
38114c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
38124c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
38132c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera        public void replaceAll(UnaryOperator<E> operator) {
38142c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera            Objects.requireNonNull(operator);
38152c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera            E[] a = this.a;
38162c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera            for (int i = 0; i < a.length; i++) {
38172c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera                a[i] = operator.apply(a[i]);
38182c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera            }
38192c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera        }
38202c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera
38212c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera        @Override
3822839c3929d114963f4da459267bdd29b4837c290eTobias Thierer        public void sort(Comparator<? super E> c) {
3823839c3929d114963f4da459267bdd29b4837c290eTobias Thierer            Arrays.sort(a, c);
38244c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        }
382551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
382651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
382751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
382851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
382951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>long</tt> arrays <tt>a</tt> and <tt>b</tt>
383051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
383151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
383251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
383351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
383451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
383551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Long}
383651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
383751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
383851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
383951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
384051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
384151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
384251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
384351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(long a[]) {
384451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
384551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
384651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
384751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
384851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (long element : a) {
384951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int elementHash = (int)(element ^ (element >>> 32));
385051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + elementHash;
385151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
385251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
385351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
385451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
385551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
385651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
385751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
385851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two non-null <tt>int</tt> arrays <tt>a</tt> and <tt>b</tt>
385951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
386051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
386151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
386251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
386351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
386451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Integer}
386551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
386651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
386751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
386851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
386951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
387051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
387151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
387251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(int a[]) {
387351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
387451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
387551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
387651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
387751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int element : a)
387851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + element;
387951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
388051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
388151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
388251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
388351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
388451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
388551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>short</tt> arrays <tt>a</tt> and <tt>b</tt>
388651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
388751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
388851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
388951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
389051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
389151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Short}
389251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
389351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
389451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
389551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
389651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
389751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
389851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
389951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(short a[]) {
390051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
390151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
390251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
390351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
390451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (short element : a)
390551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + element;
390651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
390751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
390851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
390951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
391051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
391151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
391251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>char</tt> arrays <tt>a</tt> and <tt>b</tt>
391351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
391451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
391551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
391651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
391751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
391851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Character}
391951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
392051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
392151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
392251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
392351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
392451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
392551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
392651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(char a[]) {
392751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
392851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
392951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
393051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
393151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (char element : a)
393251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + element;
393351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
393451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
393551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
393651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
393751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
393851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
393951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>byte</tt> arrays <tt>a</tt> and <tt>b</tt>
394051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
394151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
394251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
394351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
394451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
394551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Byte}
394651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
394751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
394851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
394951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
395051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
395151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
395251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
395351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(byte a[]) {
395451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
395551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
395651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
395751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
395851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (byte element : a)
395951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + element;
396051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
396151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
396251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
396351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
396451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
396551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
396651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>boolean</tt> arrays <tt>a</tt> and <tt>b</tt>
396751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
396851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
396951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
397051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
397151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
397251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Boolean}
397351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
397451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
397551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
397651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
397751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
397851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
397951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
398051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(boolean a[]) {
398151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
398251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
398351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
398451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
398551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (boolean element : a)
398651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + (element ? 1231 : 1237);
398751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
398851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
398951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
399051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
399151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
399251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
399351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>float</tt> arrays <tt>a</tt> and <tt>b</tt>
399451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
399551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
399651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
399751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
399851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
399951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Float}
400051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
400151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
400251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
400351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
400451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
400551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
400651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
400751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(float a[]) {
400851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
400951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
401051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
401151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
401251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (float element : a)
401351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + Float.floatToIntBits(element);
401451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
401551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
401651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
401751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
401851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
401951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
402051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>double</tt> arrays <tt>a</tt> and <tt>b</tt>
402151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
402251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
402351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
402451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
402551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
402651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Double}
402751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
402851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
402951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
403051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
403151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
403251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
403351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
403451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(double a[]) {
403551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
403651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
403751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
403851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
403951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (double element : a) {
404051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            long bits = Double.doubleToLongBits(element);
404151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + (int)(bits ^ (bits >>> 32));
404251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
404351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
404451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
404551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
404651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
404751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.  If
404851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the array contains other arrays as elements, the hash code is based on
404951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * their identities rather than their contents.  It is therefore
405051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * acceptable to invoke this method on an array that contains itself as an
405151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * element,  either directly or indirectly through one or more levels of
405251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
405351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
405451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
405551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.equals(a, b)</tt>, it is also the case that
405651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
405751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
405851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is equal to the value that would
405951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * be returned by <tt>Arrays.asList(a).hashCode()</tt>, unless <tt>a</tt>
406051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>, in which case <tt>0</tt> is returned.
406151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
406251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose content-based hash code to compute
406351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
406451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #deepHashCode(Object[])
406551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
406651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
406751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(Object a[]) {
406851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
406951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
407051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
407151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
407251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
407351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (Object element : a)
407451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + (element == null ? 0 : element.hashCode());
407551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
407651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
407751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
407851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
407951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
408051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the "deep contents" of the specified
408151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array.  If the array contains other arrays as elements, the
408251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * hash code is based on their contents and so on, ad infinitum.
408351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * It is therefore unacceptable to invoke this method on an array that
408451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contains itself as an element, either directly or indirectly through
408551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one or more levels of arrays.  The behavior of such an invocation is
408651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * undefined.
408751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
408851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
408951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.deepEquals(a, b)</tt>, it is also the case that
409051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.deepHashCode(a) == Arrays.deepHashCode(b)</tt>.
409151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
409251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The computation of the value returned by this method is similar to
409351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * that of the value returned by {@link List#hashCode()} on a list
409451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * containing the same elements as <tt>a</tt> in the same order, with one
409551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * difference: If an element <tt>e</tt> of <tt>a</tt> is itself an array,
409651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * its hash code is computed not by calling <tt>e.hashCode()</tt>, but as
409751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by calling the appropriate overloading of <tt>Arrays.hashCode(e)</tt>
409851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * if <tt>e</tt> is an array of a primitive type, or as by calling
409951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.deepHashCode(e)</tt> recursively if <tt>e</tt> is an array
410051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of a reference type.  If <tt>a</tt> is <tt>null</tt>, this method
410151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returns 0.
410251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
410351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose deep-content-based hash code to compute
410451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a deep-content-based hash code for <tt>a</tt>
410551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #hashCode(Object[])
410651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
410751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
410851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int deepHashCode(Object a[]) {
410951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
411051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
411151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
411251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
411351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
411451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (Object element : a) {
411551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int elementHash = 0;
41161d70b849467dfcfa30f495e6d03bcbdef47b2588Tobias Thierer            // BEGIN Android-changed: getComponentType() is faster than instanceof()
4117669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            if (element != null) {
4118669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                Class<?> cl = element.getClass().getComponentType();
4119669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                if (cl == null)
4120669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = element.hashCode();
4121669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (element instanceof Object[])
4122669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = deepHashCode((Object[]) element);
4123669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == byte.class)
4124669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((byte[]) element);
4125669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == short.class)
4126669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((short[]) element);
4127669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == int.class)
4128669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((int[]) element);
4129669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == long.class)
4130669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((long[]) element);
4131669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == char.class)
4132669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((char[]) element);
4133669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == float.class)
4134669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((float[]) element);
4135669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == double.class)
4136669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((double[]) element);
4137669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == boolean.class)
4138669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((boolean[]) element);
4139669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else
4140669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = element.hashCode();
4141669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            }
41421d70b849467dfcfa30f495e6d03bcbdef47b2588Tobias Thierer            // END Android-changed: getComponentType() is faster than instanceof()
414351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + elementHash;
414451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
414551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
414651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
414751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
414851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
414951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
415051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays are <i>deeply
415151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equal</i> to one another.  Unlike the {@link #equals(Object[],Object[])}
415251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method, this method is appropriate for use with nested arrays of
415351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arbitrary depth.
415451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
415551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Two array references are considered deeply equal if both
415651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are <tt>null</tt>, or if they refer to arrays that contain the same
415751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * number of elements and all corresponding pairs of elements in the two
415851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays are deeply equal.
415951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
416051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Two possibly <tt>null</tt> elements <tt>e1</tt> and <tt>e2</tt> are
416151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * deeply equal if any of the following conditions hold:
416251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <ul>
416351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    <li> <tt>e1</tt> and <tt>e2</tt> are both arrays of object reference
416451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         types, and <tt>Arrays.deepEquals(e1, e2) would return true</tt>
416551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    <li> <tt>e1</tt> and <tt>e2</tt> are arrays of the same primitive
416651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         type, and the appropriate overloading of
416751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>Arrays.equals(e1, e2)</tt> would return true.
416851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    <li> <tt>e1 == e2</tt>
416951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    <li> <tt>e1.equals(e2)</tt> would return true.
417051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </ul>
417151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Note that this definition permits <tt>null</tt> elements at any depth.
417251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
417351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If either of the specified arrays contain themselves as elements
417451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * either directly or indirectly through one or more levels of arrays,
417551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the behavior of this method is undefined.
417651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
417751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a1 one array to be tested for equality
417851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
417951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
418051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #equals(Object[],Object[])
418151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see Objects#deepEquals(Object, Object)
418251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
418351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
418451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean deepEquals(Object[] a1, Object[] a2) {
418551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a1 == a2)
418651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
418751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a1 == null || a2==null)
418851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
418951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a1.length;
419051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
419151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
419251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
419351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; i < length; i++) {
419451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object e1 = a1[i];
419551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object e2 = a2[i];
419651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
419751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (e1 == e2)
419851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                continue;
4199839c3929d114963f4da459267bdd29b4837c290eTobias Thierer            // Android-changed: Return early if e2 == null
4200669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            if (e1 == null || e2 == null)
420151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
420251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
420351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            // Figure out whether the two elements are equal
420451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            boolean eq = deepEquals0(e1, e2);
420551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
420651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (!eq)
420751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
420851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
420951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
421051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
421151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
421251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    static boolean deepEquals0(Object e1, Object e2) {
42131d70b849467dfcfa30f495e6d03bcbdef47b2588Tobias Thierer        // BEGIN Android-changed: getComponentType() is faster than instanceof()
4214669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        Class<?> cl1 = e1.getClass().getComponentType();
4215669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        Class<?> cl2 = e2.getClass().getComponentType();
4216669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak
4217669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        if (cl1 != cl2) {
4218669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return false;
4219669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        }
4220669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        if (e1 instanceof Object[])
4221669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return deepEquals ((Object[]) e1, (Object[]) e2);
4222669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == byte.class)
4223669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((byte[]) e1, (byte[]) e2);
4224669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == short.class)
4225669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((short[]) e1, (short[]) e2);
4226669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == int.class)
4227669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((int[]) e1, (int[]) e2);
4228669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == long.class)
4229669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((long[]) e1, (long[]) e2);
4230669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == char.class)
4231669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((char[]) e1, (char[]) e2);
4232669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == float.class)
4233669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((float[]) e1, (float[]) e2);
4234669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == double.class)
4235669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((double[]) e1, (double[]) e2);
4236669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == boolean.class)
4237669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((boolean[]) e1, (boolean[]) e2);
423851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
4239669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return e1.equals(e2);
42401d70b849467dfcfa30f495e6d03bcbdef47b2588Tobias Thierer        // END Android-changed: getComponentType() is faster than instanceof()
424151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
424251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
424351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
424451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
424551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
424651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
424751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
424851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
424951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(long)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
425051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
425151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
425251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
425351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
425451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
425551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
425651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(long[] a) {
425751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
425851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
425951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
426051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
426151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
426251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
426351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
426451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
426551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
426651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
426751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
426851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
426951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
427051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
427151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
427251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
427351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
427451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
427551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
427651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
427751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
427851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
427951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(int)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt> is
428051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>null</tt>.
428151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
428251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
428351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
428451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
428551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
428651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(int[] a) {
428751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
428851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
428951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
429051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
429151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
429251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
429351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
429451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
429551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
429651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
429751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
429851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
429951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
430051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
430151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
430251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
430351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
430451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
430551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
430651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
430751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
430851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
430951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(short)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
431051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
431151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
431251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
431351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
431451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
431551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
431651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(short[] a) {
431751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
431851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
431951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
432051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
432151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
432251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
432351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
432451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
432551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
432651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
432751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
432851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
432951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
433051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
433151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
433251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
433351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
433451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
433551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
433651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
433751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
433851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
433951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(char)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
434051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
434151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
434251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
434351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
434451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
434551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
434651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(char[] a) {
434751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
434851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
434951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
435051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
435151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
435251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
435351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
435451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
435551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
435651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
435751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
435851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
435951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
436051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
436151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
436251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
436351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
436451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
436551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
436651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements
436751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are separated by the characters <tt>", "</tt> (a comma followed
436851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by a space).  Elements are converted to strings as by
436951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(byte)</tt>.  Returns <tt>"null"</tt> if
437051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>a</tt> is <tt>null</tt>.
437151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
437251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
437351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
437451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
437551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
437651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(byte[] a) {
437751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
437851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
437951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
438051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
438151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
438251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
438351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
438451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
438551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
438651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
438751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
438851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
438951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
439051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
439151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
439251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
439351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
439451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
439551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
439651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
439751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
439851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
439951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(boolean)</tt>.  Returns <tt>"null"</tt> if
440051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>a</tt> is <tt>null</tt>.
440151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
440251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
440351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
440451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
440551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
440651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(boolean[] a) {
440751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
440851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
440951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
441051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
441151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
441251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
441351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
441451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
441551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
441651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
441751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
441851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
441951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
442051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
442151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
442251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
442351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
442451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
442551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
442651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
442751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
442851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
442951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(float)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
443051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
443151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
443251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
443351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
443451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
443551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
443651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(float[] a) {
443751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
443851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
443951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
444051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
444151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
444251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
444351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
444451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
444551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
444651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
444751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
444851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
444951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
445051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
445151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
445251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
445351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
445451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
445551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
445651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
445751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
445851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
445951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
446051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(double)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
446151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
446251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
446351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
446451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
446551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
446651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
446751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(double[] a) {
446851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
446951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
447051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
447151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
447251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
447351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
447451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
447551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
447651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
447751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
447851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
447951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
448051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
448151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
448251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
448351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
448451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
448551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
448651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the array contains other arrays as elements, they are converted to
448751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * strings by the {@link Object#toString} method inherited from
448851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Object</tt>, which describes their <i>identities</i> rather than
448951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * their contents.
449051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
449151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is equal to the value that would
449251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * be returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt>
449351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>, in which case <tt>"null"</tt> is returned.
449451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
449551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
449651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
449751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #deepToString(Object[])
449851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
449951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
450051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(Object[] a) {
450151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
450251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
450351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
450451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
450551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
450651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
450751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
450851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
450951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
451051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
451151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(String.valueOf(a[i]));
451251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
451351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
451451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
451551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
451651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
451751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
451851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
451951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the "deep contents" of the specified
452051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array.  If the array contains other arrays as elements, the string
452151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * representation contains their contents and so on.  This method is
452251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * designed for converting multidimensional arrays to strings.
452351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
452451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The string representation consists of a list of the array's
452551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent
452651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements are separated by the characters <tt>", "</tt> (a comma
452751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * followed by a space).  Elements are converted to strings as by
452851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(Object)</tt>, unless they are themselves
452951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
453051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
453151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If an element <tt>e</tt> is an array of a primitive type, it is
453251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * converted to a string as by invoking the appropriate overloading of
453351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.toString(e)</tt>.  If an element <tt>e</tt> is an array of a
453451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * reference type, it is converted to a string as by invoking
453551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this method recursively.
453651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
453751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>To avoid infinite recursion, if the specified array contains itself
453851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * as an element, or contains an indirect reference to itself through one
453951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * or more levels of arrays, the self-reference is converted to the string
454051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>"[...]"</tt>.  For example, an array containing only a reference
454151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to itself would be rendered as <tt>"[[...]]"</tt>.
454251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
454351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This method returns <tt>"null"</tt> if the specified array
454451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
454551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
454651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
454751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
454851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #toString(Object[])
454951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
455051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
455151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String deepToString(Object[] a) {
455251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
455351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
455451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
455551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int bufLen = 20 * a.length;
455651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a.length != 0 && bufLen <= 0)
455751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            bufLen = Integer.MAX_VALUE;
455851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder buf = new StringBuilder(bufLen);
455951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        deepToString(a, buf, new HashSet<Object[]>());
456051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return buf.toString();
456151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
456251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
456351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void deepToString(Object[] a, StringBuilder buf,
456451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     Set<Object[]> dejaVu) {
456551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null) {
456651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            buf.append("null");
456751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
456851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
456951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
457051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1) {
457151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            buf.append("[]");
457251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
457351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
457451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
457551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        dejaVu.add(a);
457651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        buf.append('[');
457751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
457851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
457951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object element = a[i];
458051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (element == null) {
458151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                buf.append("null");
458251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            } else {
4583839c3929d114963f4da459267bdd29b4837c290eTobias Thierer                Class<?> eClass = element.getClass();
458451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
458551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                if (eClass.isArray()) {
458651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    if (eClass == byte[].class)
458751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((byte[]) element));
458851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == short[].class)
458951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((short[]) element));
459051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == int[].class)
459151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((int[]) element));
459251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == long[].class)
459351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((long[]) element));
459451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == char[].class)
459551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((char[]) element));
459651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == float[].class)
459751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((float[]) element));
459851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == double[].class)
459951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((double[]) element));
460051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == boolean[].class)
460151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((boolean[]) element));
460251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else { // element is an array of object references
460351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        if (dejaVu.contains(element))
460451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                            buf.append("[...]");
460551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        else
460651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                            deepToString((Object[])element, buf, dejaVu);
460751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    }
460851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                } else {  // element is non-null and not an array
460951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    buf.append(element.toString());
461051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                }
461151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
461251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
461351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                break;
461451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            buf.append(", ");
461551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
461651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        buf.append(']');
461751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        dejaVu.remove(a);
461851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
4619983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski
4620839c3929d114963f4da459267bdd29b4837c290eTobias Thierer
4621983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski    /**
4622e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * Set all elements of the specified array, using the provided
4623e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * generator function to compute each element.
4624e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     *
4625e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * <p>If the generator function throws an exception, it is relayed to
4626e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * the caller and the array is left in an indeterminate state.
4627e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     *
4628e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @param <T> type of elements of the array
4629e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @param array array to be initialized
4630e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @param generator a function accepting an index and producing the desired
4631e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     *        value for that position
4632e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @throws NullPointerException if the generator is null
4633e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @since 1.8
4634e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     */
4635e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera    public static <T> void setAll(T[] array, IntFunction<? extends T> generator) {
4636e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera        Objects.requireNonNull(generator);
4637e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera        for (int i = 0; i < array.length; i++)
4638e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera            array[i] = generator.apply(i);
4639e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera    }
4640e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera
4641e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera    /**
4642501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * Set all elements of the specified array, in parallel, using the
4643501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * provided generator function to compute each element.
4644501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     *
4645501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * <p>If the generator function throws an exception, an unchecked exception
4646501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * is thrown from {@code parallelSetAll} and the array is left in an
4647501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * indeterminate state.
4648501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     *
4649501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @param <T> type of elements of the array
4650501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @param array array to be initialized
4651501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @param generator a function accepting an index and producing the desired
4652501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     *        value for that position
4653501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @throws NullPointerException if the generator is null
4654501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @since 1.8
4655501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     */
4656501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera    public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) {
4657501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera        Objects.requireNonNull(generator);
4658501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); });
4659501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera    }
4660501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera
4661501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera    /**
4662e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * Set all elements of the specified array, using the provided
4663e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * generator function to compute each element.
4664e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     *
4665e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * <p>If the generator function throws an exception, it is relayed to
4666e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * the caller and the array is left in an indeterminate state.
4667e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     *
4668e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @param array array to be initialized
4669e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @param generator a function accepting an index and producing the desired
4670e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     *        value for that position
4671e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @throws NullPointerException if the generator is null
4672e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @since 1.8
4673e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     */
4674e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera    public static void setAll(int[] array, IntUnaryOperator generator) {
4675e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera        Objects.requireNonNull(generator);
4676e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera        for (int i = 0; i < array.length; i++)
4677e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera            array[i] = generator.applyAsInt(i);
4678e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera    }
4679e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera
4680e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera    /**
4681501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * Set all elements of the specified array, in parallel, using the
4682501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * provided generator function to compute each element.
4683501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     *
4684501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * <p>If the generator function throws an exception, an unchecked exception
4685501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * is thrown from {@code parallelSetAll} and the array is left in an
4686501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * indeterminate state.
4687501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     *
4688501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @param array array to be initialized
4689501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @param generator a function accepting an index and producing the desired
4690501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * value for that position
4691501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @throws NullPointerException if the generator is null
4692501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @since 1.8
4693501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     */
4694501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera    public static void parallelSetAll(int[] array, IntUnaryOperator generator) {
4695501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera        Objects.requireNonNull(generator);
4696501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); });
4697501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera    }
4698501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera
4699501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera    /**
4700e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * Set all elements of the specified array, using the provided
4701e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * generator function to compute each element.
4702e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     *
4703e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * <p>If the generator function throws an exception, it is relayed to
4704e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * the caller and the array is left in an indeterminate state.
4705e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     *
4706e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @param array array to be initialized
4707e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @param generator a function accepting an index and producing the desired
4708e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     *        value for that position
4709e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @throws NullPointerException if the generator is null
4710e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @since 1.8
4711e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     */
4712e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera    public static void setAll(long[] array, IntToLongFunction generator) {
4713e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera        Objects.requireNonNull(generator);
4714e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera        for (int i = 0; i < array.length; i++)
4715e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera            array[i] = generator.applyAsLong(i);
4716e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera    }
4717e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera
4718e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera    /**
4719501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * Set all elements of the specified array, in parallel, using the
4720501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * provided generator function to compute each element.
4721501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     *
4722501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * <p>If the generator function throws an exception, an unchecked exception
4723501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * is thrown from {@code parallelSetAll} and the array is left in an
4724501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * indeterminate state.
4725501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     *
4726501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @param array array to be initialized
4727501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @param generator a function accepting an index and producing the desired
4728501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     *        value for that position
4729501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @throws NullPointerException if the generator is null
4730501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @since 1.8
4731501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     */
4732501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera    public static void parallelSetAll(long[] array, IntToLongFunction generator) {
4733501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera        Objects.requireNonNull(generator);
4734501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); });
4735501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera    }
4736501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera
4737501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera    /**
4738e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * Set all elements of the specified array, using the provided
4739e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * generator function to compute each element.
4740e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     *
4741e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * <p>If the generator function throws an exception, it is relayed to
4742e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * the caller and the array is left in an indeterminate state.
4743e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     *
4744e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @param array array to be initialized
4745e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @param generator a function accepting an index and producing the desired
4746e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     *        value for that position
4747e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @throws NullPointerException if the generator is null
4748e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     * @since 1.8
4749e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera     */
4750e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera    public static void setAll(double[] array, IntToDoubleFunction generator) {
4751e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera        Objects.requireNonNull(generator);
4752e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera        for (int i = 0; i < array.length; i++)
4753e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera            array[i] = generator.applyAsDouble(i);
4754e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera    }
4755e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera
4756e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera    /**
4757501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * Set all elements of the specified array, in parallel, using the
4758501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * provided generator function to compute each element.
4759501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     *
4760501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * <p>If the generator function throws an exception, an unchecked exception
4761501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * is thrown from {@code parallelSetAll} and the array is left in an
4762501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * indeterminate state.
4763501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     *
4764501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @param array array to be initialized
4765501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @param generator a function accepting an index and producing the desired
4766501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     *        value for that position
4767501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @throws NullPointerException if the generator is null
4768501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     * @since 1.8
4769501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera     */
4770501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera    public static void parallelSetAll(double[] array, IntToDoubleFunction generator) {
4771501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera        Objects.requireNonNull(generator);
4772501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); });
4773501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera    }
4774501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera
4775501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera    /**
47764c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator} covering all of the specified array.
47774c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
47784c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
47794c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
47804c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
47814c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
47824c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param <T> type of elements
47834c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
47844c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
47854c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
47864c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
47874c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static <T> Spliterator<T> spliterator(T[] array) {
47884c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array,
47894c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
47904c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
47914c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
47924c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
47934c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator} covering the specified range of the
47944c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * specified array.
47954c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
47964c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
47974c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
47984c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
47994c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
48004c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param <T> type of elements
48014c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
48024c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param startInclusive the first index to cover, inclusive
48034c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param endExclusive index immediately past the last index to cover
48044c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
48054c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
48064c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         negative, {@code endExclusive} is less than
48074c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         {@code startInclusive}, or {@code endExclusive} is greater than
48084c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         the array size
48094c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
48104c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
48114c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) {
48124c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array, startInclusive, endExclusive,
48134c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
48144c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
48154c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
48164c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
48174c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator.OfInt} covering all of the specified array.
48184c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
48194c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
48204c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
48214c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
48224c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
48234c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
48244c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
48254c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
48264c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
48274c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static Spliterator.OfInt spliterator(int[] array) {
48284c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array,
48294c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
48304c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
48314c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
48324c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
48334c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator.OfInt} covering the specified range of the
48344c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * specified array.
48354c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
48364c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
48374c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
48384c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
48394c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
48404c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
48414c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param startInclusive the first index to cover, inclusive
48424c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param endExclusive index immediately past the last index to cover
48434c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
48444c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
48454c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         negative, {@code endExclusive} is less than
48464c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         {@code startInclusive}, or {@code endExclusive} is greater than
48474c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         the array size
48484c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
48494c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
48504c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) {
48514c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array, startInclusive, endExclusive,
48524c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
48534c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
48544c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
48554c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
48564c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator.OfLong} covering all of the specified array.
48574c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
48584c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
48594c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
48604c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
48614c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
48624c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
48634c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return the spliterator for the array elements
48644c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
48654c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
48664c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static Spliterator.OfLong spliterator(long[] array) {
48674c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array,
48684c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
48694c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
48704c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
48714c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
48724c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator.OfLong} covering the specified range of the
48734c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * specified array.
48744c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
48754c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
48764c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
48774c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
48784c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
48794c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
48804c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param startInclusive the first index to cover, inclusive
48814c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param endExclusive index immediately past the last index to cover
48824c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
48834c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
48844c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         negative, {@code endExclusive} is less than
48854c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         {@code startInclusive}, or {@code endExclusive} is greater than
48864c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         the array size
48874c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
48884c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
48894c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) {
48904c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array, startInclusive, endExclusive,
48914c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
48924c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
48934c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
48944c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
48954c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator.OfDouble} covering all of the specified
48964c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * array.
48974c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
48984c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
48994c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
49004c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
49014c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
49024c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
49034c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
49044c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
49054c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
49064c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static Spliterator.OfDouble spliterator(double[] array) {
49074c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array,
49084c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
49094c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
49104c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
49114c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
49124c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator.OfDouble} covering the specified range of
49134c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * the specified array.
49144c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
49154c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
49164c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
49174c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
49184c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
49194c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
49204c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param startInclusive the first index to cover, inclusive
49214c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param endExclusive index immediately past the last index to cover
49224c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
49234c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
49244c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         negative, {@code endExclusive} is less than
49254c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         {@code startInclusive}, or {@code endExclusive} is greater than
49264c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         the array size
49274c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
49284c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
49294c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) {
49304c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array, startInclusive, endExclusive,
49314c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
49324c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
4933ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin
4934ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    /**
4935ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * Returns a sequential {@link Stream} with the specified array as its
4936ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * source.
4937ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *
4938ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param <T> The type of the array elements
4939ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param array The array, assumed to be unmodified during use
4940ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @return a {@code Stream} for the array
4941ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @since 1.8
4942ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     */
4943ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    public static <T> Stream<T> stream(T[] array) {
4944ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin        return stream(array, 0, array.length);
4945ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    }
4946ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin
4947ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    /**
4948ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * Returns a sequential {@link Stream} with the specified range of the
4949ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * specified array as its source.
4950ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *
4951ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param <T> the type of the array elements
4952ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param array the array, assumed to be unmodified during use
4953ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param startInclusive the first index to cover, inclusive
4954ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param endExclusive index immediately past the last index to cover
4955ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @return a {@code Stream} for the array range
4956ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
4957ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *         negative, {@code endExclusive} is less than
4958ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *         {@code startInclusive}, or {@code endExclusive} is greater than
4959ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *         the array size
4960ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @since 1.8
4961ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     */
4962ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive) {
4963ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin        return StreamSupport.stream(spliterator(array, startInclusive, endExclusive), false);
4964ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    }
4965ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin
4966ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    /**
4967ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * Returns a sequential {@link IntStream} with the specified array as its
4968ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * source.
4969ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *
4970ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param array the array, assumed to be unmodified during use
4971ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @return an {@code IntStream} for the array
4972ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @since 1.8
4973ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     */
4974ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    public static IntStream stream(int[] array) {
4975ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin        return stream(array, 0, array.length);
4976ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    }
4977ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin
4978ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    /**
4979ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * Returns a sequential {@link IntStream} with the specified range of the
4980ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * specified array as its source.
4981ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *
4982ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param array the array, assumed to be unmodified during use
4983ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param startInclusive the first index to cover, inclusive
4984ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param endExclusive index immediately past the last index to cover
4985ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @return an {@code IntStream} for the array range
4986ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
4987ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *         negative, {@code endExclusive} is less than
4988ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *         {@code startInclusive}, or {@code endExclusive} is greater than
4989ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *         the array size
4990ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @since 1.8
4991ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     */
4992ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    public static IntStream stream(int[] array, int startInclusive, int endExclusive) {
4993ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin        return StreamSupport.intStream(spliterator(array, startInclusive, endExclusive), false);
4994ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    }
4995ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin
4996ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    /**
4997ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * Returns a sequential {@link LongStream} with the specified array as its
4998ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * source.
4999ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *
5000ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param array the array, assumed to be unmodified during use
5001ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @return a {@code LongStream} for the array
5002ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @since 1.8
5003ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     */
5004ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    public static LongStream stream(long[] array) {
5005ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin        return stream(array, 0, array.length);
5006ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    }
5007ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin
5008ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    /**
5009ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * Returns a sequential {@link LongStream} with the specified range of the
5010ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * specified array as its source.
5011ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *
5012ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param array the array, assumed to be unmodified during use
5013ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param startInclusive the first index to cover, inclusive
5014ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param endExclusive index immediately past the last index to cover
5015ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @return a {@code LongStream} for the array range
5016ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
5017ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *         negative, {@code endExclusive} is less than
5018ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *         {@code startInclusive}, or {@code endExclusive} is greater than
5019ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *         the array size
5020ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @since 1.8
5021ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     */
5022ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    public static LongStream stream(long[] array, int startInclusive, int endExclusive) {
5023ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin        return StreamSupport.longStream(spliterator(array, startInclusive, endExclusive), false);
5024ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    }
5025ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin
5026ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    /**
5027ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * Returns a sequential {@link DoubleStream} with the specified array as its
5028ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * source.
5029ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *
5030ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param array the array, assumed to be unmodified during use
5031ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @return a {@code DoubleStream} for the array
5032ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @since 1.8
5033ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     */
5034ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    public static DoubleStream stream(double[] array) {
5035ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin        return stream(array, 0, array.length);
5036ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    }
5037ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin
5038ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    /**
5039ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * Returns a sequential {@link DoubleStream} with the specified range of the
5040ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * specified array as its source.
5041ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *
5042ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param array the array, assumed to be unmodified during use
5043ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param startInclusive the first index to cover, inclusive
5044ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @param endExclusive index immediately past the last index to cover
5045ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @return a {@code DoubleStream} for the array range
5046ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
5047ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *         negative, {@code endExclusive} is less than
5048ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *         {@code startInclusive}, or {@code endExclusive} is greater than
5049ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     *         the array size
5050ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     * @since 1.8
5051ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin     */
5052ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    public static DoubleStream stream(double[] array, int startInclusive, int endExclusive) {
5053ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin        return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false);
5054ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin    }
505551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski}
5056