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 2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.*; 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 */ 81471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static final int MIN_ARRAY_SORT_GRAN = 1 << 13; 82471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 83471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 84471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 85471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * A comparator that implements the natural ordering of a group of 86471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * mutually comparable elements. May be used when a supplied 87471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * comparator is null. To simplify code-sharing within underlying 88471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * implementations, the compare method only declares type Object 89471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * for its second argument. 90471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 91471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays class implementor's note: It is an empirical matter 92471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * whether ComparableTimSort offers any performance benefit over 93471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * TimSort used with this comparator. If not, you are better off 94471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * deleting or bypassing ComparableTimSort. There is currently no 95471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * empirical case for separating them for parallel sorting, so all 96471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * public Object parallelSort methods use the same comparator 97471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * based implementation. 98471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 99471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak static final class NaturalOrder implements Comparator<Object> { 100471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak @SuppressWarnings("unchecked") 101471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public int compare(Object first, Object second) { 102471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak return ((Comparable<Object>)first).compareTo(second); 103471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 104471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak static final NaturalOrder INSTANCE = new NaturalOrder(); 105471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 106471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Suppresses default constructor, ensuring non-instantiability. 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private Arrays() {} 10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorting of primitive type arrays. 11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(int[] a) { 126c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 13651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 13751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 13951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 14351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 14651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 14851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 14951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(int[] a, int fromIndex, int toIndex) { 15051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 151c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 16451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(long[] a) { 166c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 17151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 17251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 17351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 17451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 17651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 17751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 17851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 17951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 18051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 18151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 18251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 18351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 18451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 18551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 18651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 18751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 18851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 18951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(long[] a, int fromIndex, int toIndex) { 19051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 191c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 19451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 19751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 19851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 19951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 20051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 20151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(short[] a) { 206c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 22351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 22651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(short[] a, int fromIndex, int toIndex) { 23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 231c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 23851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 24151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 24451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 24551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(char[] a) { 246c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 24751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 24851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 24951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 25051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 26151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 26251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 26651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 26751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(char[] a, int fromIndex, int toIndex) { 27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 271c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(byte[] a) { 286c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1); 28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(byte[] a, int fromIndex, int toIndex) { 31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); 31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The {@code <} relation does not provide a total order on all float 31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 31951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value compares neither less than, greater than, nor equal to any value, 32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * even itself. This method uses the total order imposed by the method 32151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 32251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0.0f} and {@code Float.NaN} is considered greater than any 32351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * other value and all {@code Float.NaN} values are considered equal. 32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(float[] a) { 334c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 34251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The {@code <} relation does not provide a total order on all float 34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value compares neither less than, greater than, nor equal to any value, 34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * even itself. This method uses the total order imposed by the method 34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0.0f} and {@code Float.NaN} is considered greater than any 34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * other value and all {@code Float.NaN} values are considered equal. 35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(float[] a, int fromIndex, int toIndex) { 36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 367c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The {@code <} relation does not provide a total order on all double 37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value compares neither less than, greater than, nor equal to any value, 37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * even itself. This method uses the total order imposed by the method 37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0.0d} and {@code Double.NaN} is considered greater than any 37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * other value and all {@code Double.NaN} values are considered equal. 38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(double[] a) { 390c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The {@code <} relation does not provide a total order on all double 40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value compares neither less than, greater than, nor equal to any value, 40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * even itself. This method uses the total order imposed by the method 40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 40451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0.0d} and {@code Double.NaN} is considered greater than any 40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * other value and all {@code Double.NaN} values are considered equal. 40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 40851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 40951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 41051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(double[] a, int fromIndex, int toIndex) { 42251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 423c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 42551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 426471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 427471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 428471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 429471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 430471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 431471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 432471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort} 433471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 434471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 435471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a 436471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 437471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 438471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 439471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 440471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 441471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 442471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 443471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 444471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(byte[] a) { 445471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 446471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 447471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 448471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1); 449471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 450471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJByte.Sorter 451471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new byte[n], 0, n, 0, 452471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 453471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 454471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 455471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 456471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 457471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 458471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 459471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 460471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 461471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 462471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 463471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 464471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 465471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort} 466471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 467471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 468471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a working 469471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 470471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 471471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 472471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 473471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 474471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 475471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 476471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 477471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 478471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 479471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 480471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 481471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 482471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 483471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(byte[] a, int fromIndex, int toIndex) { 484471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 485471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 486471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 487471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 488471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); 489471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 490471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJByte.Sorter 491471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new byte[n], fromIndex, n, 0, 492471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 493471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 494471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 495471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 496471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 497471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 498471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 499471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 500471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 501471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 502471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort} 503471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 504471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 505471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a 506471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 507471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 508471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 509471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 510471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 511471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 512471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 513471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 514471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(char[] a) { 515471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 516471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 517471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 518471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 519471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 520471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJChar.Sorter 521471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new char[n], 0, n, 0, 522471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 523471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 524471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 525471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 526471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 527471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 528471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 529471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 530471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 531471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 532471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak @implNote The sorting algorithm is a parallel sort-merge that breaks the 533471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 534471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 535471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort} 536471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 537471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 538471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a working 539471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 540471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 541471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 542471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 543471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 544471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 545471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 546471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 547471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 548471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 549471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 550471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 551471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 552471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 553471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(char[] a, int fromIndex, int toIndex) { 554471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 555471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 556471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 557471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 558471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 559471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 560471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJChar.Sorter 561471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new char[n], fromIndex, n, 0, 562471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 563471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 564471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 565471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 566471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 567471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 568471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 569471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 570471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 571471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 572471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort} 573471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 574471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 575471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a 576471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 577471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 578471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 579471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 580471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 581471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 582471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 583471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 584471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(short[] a) { 585471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 586471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 587471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 588471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 589471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 590471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJShort.Sorter 591471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new short[n], 0, n, 0, 592471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 593471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 594471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 595471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 596471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 597471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 598471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 599471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 600471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 601471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 602471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 603471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 604471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 605471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort} 606471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 607471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 608471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a working 609471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 610471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 611471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 612471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 613471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 614471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 615471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 616471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 617471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 618471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 619471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 620471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 621471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 622471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 623471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(short[] a, int fromIndex, int toIndex) { 624471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 625471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 626471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 627471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 628471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 629471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 630471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJShort.Sorter 631471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new short[n], fromIndex, n, 0, 632471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 633471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 634471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 635471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 636471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 637471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 638471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 639471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 640471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 641471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 642471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort} 643471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 644471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 645471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a 646471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 647471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 648471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 649471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 650471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 651471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 652471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 653471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 654471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(int[] a) { 655471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 656471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 657471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 658471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 659471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 660471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJInt.Sorter 661471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new int[n], 0, n, 0, 662471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 663471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 664471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 665471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 666471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 667471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 668471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 669471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 670471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 671471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 672471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 673471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 674471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 675471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort} 676471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 677471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 678471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a working 679471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 680471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 681471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 682471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 683471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 684471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 685471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 686471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 687471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 688471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 689471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 690471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 691471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 692471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 693471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(int[] a, int fromIndex, int toIndex) { 694471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 695471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 696471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 697471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 698471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 699471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 700471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJInt.Sorter 701471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new int[n], fromIndex, n, 0, 702471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 703471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 704471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 705471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 706471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 707471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 708471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 709471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 710471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 711471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 712471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort} 713471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 714471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 715471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a 716471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 717471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 718471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 719471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 720471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 721471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 722471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 723471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 724471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(long[] a) { 725471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 726471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 727471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 728471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 729471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 730471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJLong.Sorter 731471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new long[n], 0, n, 0, 732471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 733471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 734471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 735471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 736471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 737471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 738471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 739471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 740471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 741471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 742471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 743471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 744471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 745471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort} 746471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 747471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 748471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a working 749471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 750471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 751471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 752471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 753471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 754471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 755471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 756471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 757471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 758471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 759471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 760471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 761471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 762471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 763471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(long[] a, int fromIndex, int toIndex) { 764471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 765471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 766471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 767471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 768471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 769471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 770471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJLong.Sorter 771471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new long[n], fromIndex, n, 0, 772471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 773471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 774471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 775471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 776471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 777471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 778471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 779471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>The {@code <} relation does not provide a total order on all float 780471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 781471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * value compares neither less than, greater than, nor equal to any value, 782471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * even itself. This method uses the total order imposed by the method 783471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 784471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code 0.0f} and {@code Float.NaN} is considered greater than any 785471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * other value and all {@code Float.NaN} values are considered equal. 786471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 787471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 788471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 789471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 790471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort} 791471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 792471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 793471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a 794471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 795471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 796471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 797471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 798471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 799471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 800471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 801471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 802471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(float[] a) { 803471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 804471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 805471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 806471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 807471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 808471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJFloat.Sorter 809471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new float[n], 0, n, 0, 810471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 811471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 812471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 813471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 814471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 815471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 816471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 817471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 818471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 819471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 820471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>The {@code <} relation does not provide a total order on all float 821471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 822471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * value compares neither less than, greater than, nor equal to any value, 823471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * even itself. This method uses the total order imposed by the method 824471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 825471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code 0.0f} and {@code Float.NaN} is considered greater than any 826471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * other value and all {@code Float.NaN} values are considered equal. 827471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 828471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 829471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 830471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 831471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort} 832471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 833471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 834471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a working 835471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 836471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 837471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 838471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 839471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 840471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 841471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 842471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 843471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 844471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 845471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 846471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 847471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 848471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 849471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(float[] a, int fromIndex, int toIndex) { 850471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 851471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 852471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 853471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 854471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 855471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 856471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJFloat.Sorter 857471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new float[n], fromIndex, n, 0, 858471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 859471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 860471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 861471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 862471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 863471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 864471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 865471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>The {@code <} relation does not provide a total order on all double 866471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 867471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * value compares neither less than, greater than, nor equal to any value, 868471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * even itself. This method uses the total order imposed by the method 869471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 870471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code 0.0d} and {@code Double.NaN} is considered greater than any 871471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * other value and all {@code Double.NaN} values are considered equal. 872471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 873471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 874471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 875471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 876471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort} 877471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 878471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 879471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a 880471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 881471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 882471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 883471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 884471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 885471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 886471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 887471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 888471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(double[] a) { 889471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 890471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 891471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 892471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 893471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 894471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJDouble.Sorter 895471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new double[n], 0, n, 0, 896471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 897471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 898471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 899471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 900471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 901471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 902471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 903471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 904471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 905471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 906471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>The {@code <} relation does not provide a total order on all double 907471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 908471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * value compares neither less than, greater than, nor equal to any value, 909471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * even itself. This method uses the total order imposed by the method 910471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 911471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code 0.0d} and {@code Double.NaN} is considered greater than any 912471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * other value and all {@code Double.NaN} values are considered equal. 913471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 914471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 915471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 916471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 917471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort} 918471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 919471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 920471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a working 921471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 922471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 923471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 924471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 925471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 926471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 927471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 928471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 929471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 930471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 931471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 932471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 933471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 934471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 935471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(double[] a, int fromIndex, int toIndex) { 936471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 937471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 938471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 939471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 940471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 941471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 942471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJDouble.Sorter 943471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new double[n], fromIndex, n, 0, 944471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 945471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 946471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 947471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 948471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 949471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array of objects into ascending order, according 950471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * to the {@linkplain Comparable natural ordering} of its elements. 951471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * All elements in the array must implement the {@link Comparable} 952471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * interface. Furthermore, all elements in the array must be 953471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must 954471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not throw a {@code ClassCastException} for any elements {@code e1} 955471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * and {@code e2} in the array). 956471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 957471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 958471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not be reordered as a result of the sort. 959471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 960471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 961471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 962471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 963471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 964471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 965471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 966471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a 967471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 968471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 969471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 970471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 971471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param <T> the class of the objects to be sorted 972471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 973471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 974471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ClassCastException if the array contains elements that are not 975471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <i>mutually comparable</i> (for example, strings and integers) 976471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException (optional) if the natural 977471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * ordering of the array elements is found to violate the 978471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link Comparable} contract 979471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 980471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 981471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 982471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak @SuppressWarnings("unchecked") 983471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static <T extends Comparable<? super T>> void parallelSort(T[] a) { 984471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 985471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 986471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 987471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak TimSort.sort(a, 0, n, NaturalOrder.INSTANCE, null, 0, 0); 988471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 989471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJObject.Sorter<T> 990471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, 991471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (T[])Array.newInstance(a.getClass().getComponentType(), n), 992471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 993471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke(); 994471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 995471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 996471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 997471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the specified array of objects into 998471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * ascending order, according to the 999471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@linkplain Comparable natural ordering} of its 1000471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * elements. The range to be sorted extends from index 1001471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive. 1002471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All 1003471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * elements in this range must implement the {@link Comparable} 1004471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * interface. Furthermore, all elements in this range must be <i>mutually 1005471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a 1006471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code ClassCastException} for any elements {@code e1} and 1007471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code e2} in the array). 1008471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1009471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1010471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not be reordered as a result of the sort. 1011471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1012471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 1013471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 1014471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 1015471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 1016471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 1017471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 1018471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working 1019471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 1020471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 1021471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 1022471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1023471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param <T> the class of the objects to be sorted 1024471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 1025471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element (inclusive) to be 1026471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted 1027471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element (exclusive) to be sorted 1028471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 1029471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * (optional) if the natural ordering of the array elements is 1030471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * found to violate the {@link Comparable} contract 1031471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 1032471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code toIndex > a.length} 1033471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ClassCastException if the array contains elements that are 1034471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not <i>mutually comparable</i> (for example, strings and 1035471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * integers). 1036471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1037471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 1038471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 1039471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak @SuppressWarnings("unchecked") 1040471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static <T extends Comparable<? super T>> 1041471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak void parallelSort(T[] a, int fromIndex, int toIndex) { 1042471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 1043471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 1044471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 1045471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1046471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak TimSort.sort(a, fromIndex, toIndex, NaturalOrder.INSTANCE, null, 0, 0); 1047471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 1048471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJObject.Sorter<T> 1049471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, 1050471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (T[])Array.newInstance(a.getClass().getComponentType(), n), 1051471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1052471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke(); 1053471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 1054471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 1055471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 1056471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array of objects according to the order induced by 1057471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the specified comparator. All elements in the array must be 1058471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <i>mutually comparable</i> by the specified comparator (that is, 1059471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 1060471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * for any elements {@code e1} and {@code e2} in the array). 1061471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1062471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1063471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not be reordered as a result of the sort. 1064471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1065471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 1066471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 1067471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 1068471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 1069471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 1070471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 1071471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a 1072471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 1073471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 1074471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 1075471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1076471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param <T> the class of the objects to be sorted 1077471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 1078471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param cmp the comparator to determine the order of the array. A 1079471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code null} value indicates that the elements' 1080471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@linkplain Comparable natural ordering} should be used. 1081471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ClassCastException if the array contains elements that are 1082471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not <i>mutually comparable</i> using the specified comparator 1083471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException (optional) if the comparator is 1084471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * found to violate the {@link java.util.Comparator} contract 1085471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1086471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 1087471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 1088471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak @SuppressWarnings("unchecked") 1089471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) { 1090471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (cmp == null) 1091471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak cmp = NaturalOrder.INSTANCE; 1092471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 1093471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 1094471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1095471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak TimSort.sort(a, 0, n, cmp, null, 0, 0); 1096471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 1097471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJObject.Sorter<T> 1098471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, 1099471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (T[])Array.newInstance(a.getClass().getComponentType(), n), 1100471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1101471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g, cmp).invoke(); 1102471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 1103471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 1104471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 1105471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the specified array of objects according 1106471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * to the order induced by the specified comparator. The range to be 1107471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted extends from index {@code fromIndex}, inclusive, to index 1108471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 1109471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * range to be sorted is empty.) All elements in the range must be 1110471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <i>mutually comparable</i> by the specified comparator (that is, 1111471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 1112471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * for any elements {@code e1} and {@code e2} in the range). 1113471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1114471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1115471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not be reordered as a result of the sort. 1116471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1117471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 1118471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 1119471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 1120471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 1121471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 1122471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 1123471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working 1124471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 1125471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 1126471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 1127471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1128471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param <T> the class of the objects to be sorted 1129471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 1130471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element (inclusive) to be 1131471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted 1132471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element (exclusive) to be sorted 1133471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param cmp the comparator to determine the order of the array. A 1134471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code null} value indicates that the elements' 1135471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@linkplain Comparable natural ordering} should be used. 1136471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 1137471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * (optional) if the natural ordering of the array elements is 1138471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * found to violate the {@link Comparable} contract 1139471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 1140471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code toIndex > a.length} 1141471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ClassCastException if the array contains elements that are 1142471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not <i>mutually comparable</i> (for example, strings and 1143471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * integers). 1144471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1145471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 1146471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 1147471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak @SuppressWarnings("unchecked") 1148471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static <T> void parallelSort(T[] a, int fromIndex, int toIndex, 1149471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak Comparator<? super T> cmp) { 1150471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 1151471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (cmp == null) 1152471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak cmp = NaturalOrder.INSTANCE; 1153471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 1154471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 1155471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1156471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak TimSort.sort(a, fromIndex, toIndex, cmp, null, 0, 0); 1157471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 1158471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJObject.Sorter<T> 1159471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, 1160471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (T[])Array.newInstance(a.getClass().getComponentType(), n), 1161471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1162471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g, cmp).invoke(); 1163471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 1164471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 116551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 116651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorting of complex type arrays. 116751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 116851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 116951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 117051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Old merge sort implementation can be selected (for 117151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * compatibility with broken comparators) using a system property. 117251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Cannot be a static boolean in the enclosing class due to 117351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * circular dependencies. To be removed in a future release. 117451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 117551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static final class LegacyMergeSort { 11761ae149ae8db46adac5723c739d53dd82a1cf2e1aPiotr Jastrzebski // Android-changed: Never use circular merge sort. 11771ae149ae8db46adac5723c739d53dd82a1cf2e1aPiotr Jastrzebski private static final boolean userRequested = false; 117851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 117951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 118051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 118151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this platform has an optimizing VM, check whether ComparableTimSort 118251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers any performance benefit over TimSort in conjunction with a 118351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * comparator that returns: 118451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code ((Comparable)first).compareTo(Second)}. 118551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If not, you are better off deleting ComparableTimSort to 118651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * eliminate the code duplication. In other words, the commented 118751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * out code below is the preferable implementation for sorting 118851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays of Comparables if it offers sufficient performance. 118951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 119051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 119151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// /** 119251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// * A comparator that implements the natural ordering of a group of 119351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// * mutually comparable elements. Using this comparator saves us 119451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// * from duplicating most of the code in this file (one version for 119551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// * Comparables, one for explicit Comparators). 119651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// */ 119751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// private static final Comparator<Object> NATURAL_ORDER = 119851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// new Comparator<Object>() { 119951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// @SuppressWarnings("unchecked") 120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// public int compare(Object first, Object second) { 120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// return ((Comparable<Object>)first).compareTo(second); 120251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// } 120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// }; 120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// 120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// public static void sort(Object[] a) { 120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// sort(a, 0, a.length, NATURAL_ORDER); 120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// } 120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// 120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// public static void sort(Object[] a, int fromIndex, int toIndex) { 121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// sort(a, fromIndex, toIndex, NATURAL_ORDER); 121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// } 121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 121451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array of objects into ascending order, according 121551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the {@linkplain Comparable natural ordering} of its elements. 121651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * All elements in the array must implement the {@link Comparable} 121751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface. Furthermore, all elements in the array must be 121851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must 121951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not throw a {@code ClassCastException} for any elements {@code e1} 122051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and {@code e2} in the array). 122151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 122251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 122351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not be reordered as a result of the sort. 122451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 122551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: This implementation is a stable, adaptive, 122651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * iterative mergesort that requires far fewer than n lg(n) comparisons 122751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * when the input array is partially sorted, while offering the 122851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * performance of a traditional mergesort when the input array is 122951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * randomly ordered. If the input array is nearly sorted, the 123051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implementation requires approximately n comparisons. Temporary 123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * storage requirements vary from a small constant for nearly sorted 123251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input arrays to n/2 object references for randomly ordered input 123351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays. 123451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 123551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation takes equal advantage of ascending and 123651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * descending order in its input array, and can take advantage of 123751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ascending and descending order in different parts of the the same 123851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input array. It is well-suited to merging two or more sorted arrays: 123951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * simply concatenate the arrays and sort the resulting array. 124051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 124151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation was adapted from Tim Peters's list sort for Python 124251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 124351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic 124451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorting and Information Theoretic Complexity", in Proceedings of the 124551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 124651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * January 1993. 124751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 124851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 124951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the array contains elements that are not 125051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> (for example, strings and integers) 125151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException (optional) if the natural 125251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ordering of the array elements is found to violate the 125351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Comparable} contract 125451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 125551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(Object[] a) { 125651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (LegacyMergeSort.userRequested) 125751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski legacyMergeSort(a); 125851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 1259c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath ComparableTimSort.sort(a, 0, a.length, null, 0, 0); 126051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 126151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 126251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** To be removed in a future release. */ 126351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void legacyMergeSort(Object[] a) { 126451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object[] aux = a.clone(); 126551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(aux, a, 0, a.length, 0); 126651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 126751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 126851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 126951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the specified array of objects into 127051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ascending order, according to the 127151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} of its 127251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements. The range to be sorted extends from index 127351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive. 127451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All 127551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in this range must implement the {@link Comparable} 127651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface. Furthermore, all elements in this range must be <i>mutually 127751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a 127851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code ClassCastException} for any elements {@code e1} and 127951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code e2} in the array). 128051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 128151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 128251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not be reordered as a result of the sort. 128351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 128451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: This implementation is a stable, adaptive, 128551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * iterative mergesort that requires far fewer than n lg(n) comparisons 128651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * when the input array is partially sorted, while offering the 128751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * performance of a traditional mergesort when the input array is 128851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * randomly ordered. If the input array is nearly sorted, the 128951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implementation requires approximately n comparisons. Temporary 129051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * storage requirements vary from a small constant for nearly sorted 129151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input arrays to n/2 object references for randomly ordered input 129251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays. 129351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 129451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation takes equal advantage of ascending and 129551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * descending order in its input array, and can take advantage of 129651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ascending and descending order in different parts of the the same 129751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input array. It is well-suited to merging two or more sorted arrays: 129851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * simply concatenate the arrays and sort the resulting array. 129951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 130051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation was adapted from Tim Peters's list sort for Python 130151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 130251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic 130351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorting and Information Theoretic Complexity", in Proceedings of the 130451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 130551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * January 1993. 130651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 130751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 130851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 130951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sorted 131051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be sorted 131151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 131251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (optional) if the natural ordering of the array elements is 131351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * found to violate the {@link Comparable} contract 131451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 131551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code toIndex > a.length} 131651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the array contains elements that are 131751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not <i>mutually comparable</i> (for example, strings and 131851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * integers). 131951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 132051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(Object[] a, int fromIndex, int toIndex) { 1321c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath rangeCheck(a.length, fromIndex, toIndex); 132251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (LegacyMergeSort.userRequested) 132351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski legacyMergeSort(a, fromIndex, toIndex); 132451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 1325c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath ComparableTimSort.sort(a, fromIndex, toIndex, null, 0, 0); 132651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 132751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 132851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** To be removed in a future release. */ 132951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void legacyMergeSort(Object[] a, 133051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int fromIndex, int toIndex) { 133151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 133251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object[] aux = copyOfRange(a, fromIndex, toIndex); 133351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(aux, a, fromIndex, toIndex, -fromIndex); 133451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 133551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 133651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 133751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Tuning parameter: list size at or below which insertion sort will be 133851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * used in preference to mergesort. 133951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * To be removed in a future release. 134051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 134151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static final int INSERTIONSORT_THRESHOLD = 7; 134251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 134351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 134451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Src is the source array that starts at index 0 134551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Dest is the (possibly larger) array destination with a possible offset 134651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * low is the index in dest to start sorting 134751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * high is the end index in dest to end sorting 134851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * off is the offset to generate corresponding low, high in src 134951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * To be removed in a future release. 135051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 135151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void mergeSort(Object[] src, 135251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object[] dest, 135351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low, 135451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high, 135551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int off) { 135651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = high - low; 135751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 135851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Insertion sort on smallest arrays 135951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (length < INSERTIONSORT_THRESHOLD) { 136051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=low; i<high; i++) 136151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int j=i; j>low && 136251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--) 136351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski swap(dest, j, j-1); 136451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 136551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 136651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 136751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Recursively sort halves of dest into src 136851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int destLow = low; 136951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int destHigh = high; 137051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low += off; 137151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high += off; 137251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 137351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(dest, src, low, mid, -off); 137451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(dest, src, mid, high, -off); 137551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 137651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // If list is already sorted, just copy from src to dest. This is an 137751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // optimization that results in faster sorts for nearly ordered lists. 137851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) { 137951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(src, low, dest, destLow, length); 138051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 138151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 138251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 138351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Merge sorted halves (now in src) into dest 138451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for(int i = destLow, p = low, q = mid; i < destHigh; i++) { 138551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0) 138651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski dest[i] = src[p++]; 138751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 138851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski dest[i] = src[q++]; 138951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 139051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 139151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 139251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 139351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Swaps x[a] with x[b]. 139451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 139551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void swap(Object[] x, int a, int b) { 139651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object t = x[a]; 139751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski x[a] = x[b]; 139851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski x[b] = t; 139951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 140051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 140151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 140251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array of objects according to the order induced by 140351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified comparator. All elements in the array must be 140451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> by the specified comparator (that is, 140551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 140651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for any elements {@code e1} and {@code e2} in the array). 140751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 140851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 140951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not be reordered as a result of the sort. 141051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 141151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: This implementation is a stable, adaptive, 141251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * iterative mergesort that requires far fewer than n lg(n) comparisons 141351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * when the input array is partially sorted, while offering the 141451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * performance of a traditional mergesort when the input array is 141551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * randomly ordered. If the input array is nearly sorted, the 141651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implementation requires approximately n comparisons. Temporary 141751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * storage requirements vary from a small constant for nearly sorted 141851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input arrays to n/2 object references for randomly ordered input 141951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays. 142051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 142151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation takes equal advantage of ascending and 142251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * descending order in its input array, and can take advantage of 142351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ascending and descending order in different parts of the the same 142451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input array. It is well-suited to merging two or more sorted arrays: 142551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * simply concatenate the arrays and sort the resulting array. 142651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 142751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation was adapted from Tim Peters's list sort for Python 142851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 142951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic 143051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorting and Information Theoretic Complexity", in Proceedings of the 143151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 143251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * January 1993. 143351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 143451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 143551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param c the comparator to determine the order of the array. A 143651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} value indicates that the elements' 143751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} should be used. 143851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the array contains elements that are 143951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not <i>mutually comparable</i> using the specified comparator 144051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException (optional) if the comparator is 144151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * found to violate the {@link Comparator} contract 144251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 144351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> void sort(T[] a, Comparator<? super T> c) { 1444c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath if (c == null) { 1445c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath sort(a); 1446c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath } else { 1447c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath if (LegacyMergeSort.userRequested) 1448c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath legacyMergeSort(a, c); 1449c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath else 1450c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath TimSort.sort(a, 0, a.length, c, null, 0, 0); 1451c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath } 145251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 145351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 145451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** To be removed in a future release. */ 145551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) { 145651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] aux = a.clone(); 145751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (c==null) 145851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(aux, a, 0, a.length, 0); 145951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 146051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(aux, a, 0, a.length, 0, c); 146151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 146251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 146351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 146451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the specified array of objects according 146551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the order induced by the specified comparator. The range to be 146651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sorted extends from index {@code fromIndex}, inclusive, to index 146751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 146851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be sorted is empty.) All elements in the range must be 146951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> by the specified comparator (that is, 147051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 147151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for any elements {@code e1} and {@code e2} in the range). 147251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 147351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 147451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not be reordered as a result of the sort. 147551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 147651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: This implementation is a stable, adaptive, 147751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * iterative mergesort that requires far fewer than n lg(n) comparisons 147851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * when the input array is partially sorted, while offering the 147951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * performance of a traditional mergesort when the input array is 148051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * randomly ordered. If the input array is nearly sorted, the 148151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implementation requires approximately n comparisons. Temporary 148251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * storage requirements vary from a small constant for nearly sorted 148351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input arrays to n/2 object references for randomly ordered input 148451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays. 148551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 148651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation takes equal advantage of ascending and 148751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * descending order in its input array, and can take advantage of 148851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ascending and descending order in different parts of the the same 148951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input array. It is well-suited to merging two or more sorted arrays: 149051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * simply concatenate the arrays and sort the resulting array. 149151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 149251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation was adapted from Tim Peters's list sort for Python 149351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 149451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic 149551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorting and Information Theoretic Complexity", in Proceedings of the 149651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 149751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * January 1993. 149851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 149951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 150051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 150151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sorted 150251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be sorted 150351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param c the comparator to determine the order of the array. A 150451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} value indicates that the elements' 150551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} should be used. 150651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the array contains elements that are not 150751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> using the specified comparator. 150851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 150951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (optional) if the comparator is found to violate the 151051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Comparator} contract 151151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 151251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code toIndex > a.length} 151351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 151451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> void sort(T[] a, int fromIndex, int toIndex, 151551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Comparator<? super T> c) { 1516c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath if (c == null) { 1517c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath sort(a, fromIndex, toIndex); 1518c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath } else { 1519c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath rangeCheck(a.length, fromIndex, toIndex); 1520c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath if (LegacyMergeSort.userRequested) 1521c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath legacyMergeSort(a, fromIndex, toIndex, c); 1522c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath else 1523c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath TimSort.sort(a, fromIndex, toIndex, c, null, 0, 0); 1524c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath } 152551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 152651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 152751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** To be removed in a future release. */ 152851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static <T> void legacyMergeSort(T[] a, int fromIndex, int toIndex, 152951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Comparator<? super T> c) { 153051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 153151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] aux = copyOfRange(a, fromIndex, toIndex); 153251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (c==null) 153351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(aux, a, fromIndex, toIndex, -fromIndex); 153451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 153551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c); 153651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 153751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 153851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 153951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Src is the source array that starts at index 0 154051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Dest is the (possibly larger) array destination with a possible offset 154151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * low is the index in dest to start sorting 154251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * high is the end index in dest to end sorting 154351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * off is the offset into src corresponding to low in dest 154451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * To be removed in a future release. 154551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 154651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void mergeSort(Object[] src, 154751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object[] dest, 154851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low, int high, int off, 154951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Comparator c) { 155051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = high - low; 155151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 155251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Insertion sort on smallest arrays 155351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (length < INSERTIONSORT_THRESHOLD) { 155451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=low; i<high; i++) 155551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--) 155651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski swap(dest, j, j-1); 155751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 155851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 155951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 156051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Recursively sort halves of dest into src 156151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int destLow = low; 156251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int destHigh = high; 156351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low += off; 156451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high += off; 156551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 156651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(dest, src, low, mid, -off, c); 156751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(dest, src, mid, high, -off, c); 156851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 156951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // If list is already sorted, just copy from src to dest. This is an 157051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // optimization that results in faster sorts for nearly ordered lists. 157151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (c.compare(src[mid-1], src[mid]) <= 0) { 157251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(src, low, dest, destLow, length); 157351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 157451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 157551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 157651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Merge sorted halves (now in src) into dest 157751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for(int i = destLow, p = low, q = mid; i < destHigh; i++) { 157851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0) 157951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski dest[i] = src[p++]; 158051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 158151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski dest[i] = src[q++]; 158251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 158351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 158451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 158551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 158651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Checks that {@code fromIndex} and {@code toIndex} are in 158751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range and throws an appropriate exception, if they aren't. 158851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 158951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void rangeCheck(int length, int fromIndex, int toIndex) { 159051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (fromIndex > toIndex) { 159151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException( 159251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); 159351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 159451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (fromIndex < 0) { 159551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new ArrayIndexOutOfBoundsException(fromIndex); 159651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 159751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (toIndex > length) { 159851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new ArrayIndexOutOfBoundsException(toIndex); 159951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 160051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 160151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 16021eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong 16031eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong // Parallel prefix 16041eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong 16051eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong /** 16061eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * Cumulates, in parallel, each element of the given array in place, 16071eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * using the supplied function. For example if the array initially 16081eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * holds {@code [2, 1, 0, 3]} and the operation performs addition, 16091eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * then upon return the array holds {@code [2, 3, 3, 6]}. 16101eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * Parallel prefix computation is usually more efficient than 16111eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * sequential loops for large arrays. 16121eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * 16131eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param <T> the class of the objects in the array 16141eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param array the array, which is modified in-place by this method 16151eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param op a side-effect-free, associative function to perform the 16161eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * cumulation 16171eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws NullPointerException if the specified array or function is null 16181eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @since 1.8 16191eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong */ 16201eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op) { 16211eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong Objects.requireNonNull(op); 16221eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong if (array.length > 0) 16231eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong new ArrayPrefixHelpers.CumulateTask<> 16241eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong (null, op, array, 0, array.length).invoke(); 16251eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong } 16261eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong 16271eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong /** 16281eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * Performs {@link #parallelPrefix(Object[], BinaryOperator)} 16291eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * for the given subrange of the array. 16301eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * 16311eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param <T> the class of the objects in the array 16321eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param array the array 16331eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param fromIndex the index of the first element, inclusive 16341eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param toIndex the index of the last element, exclusive 16351eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param op a side-effect-free, associative function to perform the 16361eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * cumulation 16371eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws IllegalArgumentException if {@code fromIndex > toIndex} 16381eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws ArrayIndexOutOfBoundsException 16391eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * if {@code fromIndex < 0} or {@code toIndex > array.length} 16401eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws NullPointerException if the specified array or function is null 16411eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @since 1.8 16421eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong */ 16431eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong public static <T> void parallelPrefix(T[] array, int fromIndex, 16441eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong int toIndex, BinaryOperator<T> op) { 16451eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong Objects.requireNonNull(op); 16461eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong rangeCheck(array.length, fromIndex, toIndex); 16471eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong if (fromIndex < toIndex) 16481eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong new ArrayPrefixHelpers.CumulateTask<> 16491eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong (null, op, array, fromIndex, toIndex).invoke(); 16501eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong } 16511eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong 16521eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong /** 16531eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * Cumulates, in parallel, each element of the given array in place, 16541eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * using the supplied function. For example if the array initially 16551eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * holds {@code [2, 1, 0, 3]} and the operation performs addition, 16561eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * then upon return the array holds {@code [2, 3, 3, 6]}. 16571eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * Parallel prefix computation is usually more efficient than 16581eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * sequential loops for large arrays. 16591eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * 16601eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param array the array, which is modified in-place by this method 16611eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param op a side-effect-free, associative function to perform the 16621eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * cumulation 16631eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws NullPointerException if the specified array or function is null 16641eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @since 1.8 16651eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong */ 16661eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong public static void parallelPrefix(long[] array, LongBinaryOperator op) { 16671eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong Objects.requireNonNull(op); 16681eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong if (array.length > 0) 16691eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong new ArrayPrefixHelpers.LongCumulateTask 16701eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong (null, op, array, 0, array.length).invoke(); 16711eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong } 16721eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong 16731eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong /** 16741eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * Performs {@link #parallelPrefix(long[], LongBinaryOperator)} 16751eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * for the given subrange of the array. 16761eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * 16771eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param array the array 16781eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param fromIndex the index of the first element, inclusive 16791eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param toIndex the index of the last element, exclusive 16801eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param op a side-effect-free, associative function to perform the 16811eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * cumulation 16821eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws IllegalArgumentException if {@code fromIndex > toIndex} 16831eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws ArrayIndexOutOfBoundsException 16841eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * if {@code fromIndex < 0} or {@code toIndex > array.length} 16851eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws NullPointerException if the specified array or function is null 16861eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @since 1.8 16871eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong */ 16881eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong public static void parallelPrefix(long[] array, int fromIndex, 16891eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong int toIndex, LongBinaryOperator op) { 16901eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong Objects.requireNonNull(op); 16911eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong rangeCheck(array.length, fromIndex, toIndex); 16921eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong if (fromIndex < toIndex) 16931eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong new ArrayPrefixHelpers.LongCumulateTask 16941eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong (null, op, array, fromIndex, toIndex).invoke(); 16951eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong } 16961eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong 16971eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong /** 16981eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * Cumulates, in parallel, each element of the given array in place, 16991eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * using the supplied function. For example if the array initially 17001eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * holds {@code [2.0, 1.0, 0.0, 3.0]} and the operation performs addition, 17011eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * then upon return the array holds {@code [2.0, 3.0, 3.0, 6.0]}. 17021eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * Parallel prefix computation is usually more efficient than 17031eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * sequential loops for large arrays. 17041eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * 17051eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * <p> Because floating-point operations may not be strictly associative, 17061eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * the returned result may not be identical to the value that would be 17071eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * obtained if the operation was performed sequentially. 17081eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * 17091eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param array the array, which is modified in-place by this method 17101eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param op a side-effect-free function to perform the cumulation 17111eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws NullPointerException if the specified array or function is null 17121eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @since 1.8 17131eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong */ 17141eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong public static void parallelPrefix(double[] array, DoubleBinaryOperator op) { 17151eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong Objects.requireNonNull(op); 17161eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong if (array.length > 0) 17171eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong new ArrayPrefixHelpers.DoubleCumulateTask 17181eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong (null, op, array, 0, array.length).invoke(); 17191eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong } 17201eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong 17211eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong /** 17221eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * Performs {@link #parallelPrefix(double[], DoubleBinaryOperator)} 17231eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * for the given subrange of the array. 17241eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * 17251eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param array the array 17261eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param fromIndex the index of the first element, inclusive 17271eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param toIndex the index of the last element, exclusive 17281eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param op a side-effect-free, associative function to perform the 17291eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * cumulation 17301eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws IllegalArgumentException if {@code fromIndex > toIndex} 17311eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws ArrayIndexOutOfBoundsException 17321eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * if {@code fromIndex < 0} or {@code toIndex > array.length} 17331eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws NullPointerException if the specified array or function is null 17341eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @since 1.8 17351eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong */ 17361eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong public static void parallelPrefix(double[] array, int fromIndex, 17371eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong int toIndex, DoubleBinaryOperator op) { 17381eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong Objects.requireNonNull(op); 17391eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong rangeCheck(array.length, fromIndex, toIndex); 17401eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong if (fromIndex < toIndex) 17411eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong new ArrayPrefixHelpers.DoubleCumulateTask 17421eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong (null, op, array, fromIndex, toIndex).invoke(); 17431eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong } 17441eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong 17451eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong /** 17461eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * Cumulates, in parallel, each element of the given array in place, 17471eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * using the supplied function. For example if the array initially 17481eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * holds {@code [2, 1, 0, 3]} and the operation performs addition, 17491eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * then upon return the array holds {@code [2, 3, 3, 6]}. 17501eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * Parallel prefix computation is usually more efficient than 17511eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * sequential loops for large arrays. 17521eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * 17531eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param array the array, which is modified in-place by this method 17541eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param op a side-effect-free, associative function to perform the 17551eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * cumulation 17561eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws NullPointerException if the specified array or function is null 17571eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @since 1.8 17581eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong */ 17591eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong public static void parallelPrefix(int[] array, IntBinaryOperator op) { 17601eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong Objects.requireNonNull(op); 17611eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong if (array.length > 0) 17621eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong new ArrayPrefixHelpers.IntCumulateTask 17631eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong (null, op, array, 0, array.length).invoke(); 17641eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong } 17651eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong 17661eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong /** 17671eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * Performs {@link #parallelPrefix(int[], IntBinaryOperator)} 17681eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * for the given subrange of the array. 17691eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * 17701eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param array the array 17711eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param fromIndex the index of the first element, inclusive 17721eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param toIndex the index of the last element, exclusive 17731eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @param op a side-effect-free, associative function to perform the 17741eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * cumulation 17751eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws IllegalArgumentException if {@code fromIndex > toIndex} 17761eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws ArrayIndexOutOfBoundsException 17771eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * if {@code fromIndex < 0} or {@code toIndex > array.length} 17781eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @throws NullPointerException if the specified array or function is null 17791eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong * @since 1.8 17801eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong */ 17811eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong public static void parallelPrefix(int[] array, int fromIndex, 17821eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong int toIndex, IntBinaryOperator op) { 17831eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong Objects.requireNonNull(op); 17841eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong rangeCheck(array.length, fromIndex, toIndex); 17851eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong if (fromIndex < toIndex) 17861eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong new ArrayPrefixHelpers.IntCumulateTask 17871eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong (null, op, array, fromIndex, toIndex).invoke(); 17881eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong } 17891eae5d5c81785d589e337d408f2f3b87fdbb4c8eYi Kong 179051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Searching 179151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 179251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 179351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of longs for the specified value using the 179451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. The array must be sorted (as 179551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(long[])} method) prior to making this call. If it 179651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the array contains 179751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 179851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 179951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 180051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 180151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 180251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 180351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 180451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 180551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 180651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 180751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 180851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 180951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 181051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 181151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(long[] a, long key) { 181251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 181351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 181451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 181551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 181651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 181751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of longs for the specified value using the 181851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. 181951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted (as 182051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(long[], int, int)} method) 182151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. If it 182251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the range contains 182351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 182451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 182551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 182651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 182751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 182851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 182951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 183051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 183151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 183251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 183351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 183451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 183551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 183651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 183751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 183851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 183951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 184051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 184151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 184251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 184351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 184451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 184551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 184651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 184751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(long[] a, int fromIndex, int toIndex, 184851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long key) { 184951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 185051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 185151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 185251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 185351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 185451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(long[] a, int fromIndex, int toIndex, 185551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long key) { 185651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 185751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 185851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 185951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 186051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 186151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long midVal = a[mid]; 186251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 186351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 186451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 186551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 186651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 186751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 186851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 186951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 187051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 187151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 187251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 187351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 187451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of ints for the specified value using the 187551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. The array must be sorted (as 187651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(int[])} method) prior to making this call. If it 187751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the array contains 187851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 187951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 188051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 188151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 188251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 188351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 188451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 188551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 188651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 188751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 188851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 188951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 189051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 189151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 189251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(int[] a, int key) { 189351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 189451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 189551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 189651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 189751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 189851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of ints for the specified value using the 189951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. 190051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted (as 190151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(int[], int, int)} method) 190251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. If it 190351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the range contains 190451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 190551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 190651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 190751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 190851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 190951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 191051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 191151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 191251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 191351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 191451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 191551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 191651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 191751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 191851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 191951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 192051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 192151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 192251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 192351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 192451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 192551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 192651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 192751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 192851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(int[] a, int fromIndex, int toIndex, 192951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int key) { 193051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 193151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 193251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 193351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 193451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 193551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(int[] a, int fromIndex, int toIndex, 193651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int key) { 193751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 193851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 193951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 194051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 194151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 194251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int midVal = a[mid]; 194351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 194451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 194551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 194651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 194751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 194851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 194951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 195051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 195151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 195251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 195351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 195451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 195551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of shorts for the specified value using 195651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary search algorithm. The array must be sorted 195751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (as by the {@link #sort(short[])} method) prior to making this call. If 195851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is not sorted, the results are undefined. If the array contains 195951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 196051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 196151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 196251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 196351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 196451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 196551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 196651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 196751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 196851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 196951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 197051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 197151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 197251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 197351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(short[] a, short key) { 197451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 197551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 197651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 197751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 197851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 197951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of shorts for the specified value using 198051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary search algorithm. 198151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted 198251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (as by the {@link #sort(short[], int, int)} method) 198351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. If 198451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is not sorted, the results are undefined. If the range contains 198551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 198651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 198751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 198851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 198951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 199051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 199151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 199251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 199351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 199451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 199551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 199651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 199751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 199851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 199951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 200051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 200151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 200251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 200351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 200451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 200551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 200651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 200751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 200851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 200951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(short[] a, int fromIndex, int toIndex, 201051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski short key) { 201151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 201251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 201351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 201451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 201551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 201651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(short[] a, int fromIndex, int toIndex, 201751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski short key) { 201851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 201951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 202051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 202151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 202251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 202351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski short midVal = a[mid]; 202451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 202551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 202651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 202751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 202851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 202951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 203051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 203151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 203251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 203351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 203451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 203551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 203651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of chars for the specified value using the 203751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. The array must be sorted (as 203851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(char[])} method) prior to making this call. If it 203951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the array contains 204051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 204151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 204251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 204351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 204451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 204551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 204651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 204751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 204851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 204951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 205051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 205151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 205251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 205351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 205451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(char[] a, char key) { 205551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 205651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 205751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 205851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 205951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 206051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of chars for the specified value using the 206151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. 206251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted (as 206351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(char[], int, int)} method) 206451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. If it 206551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the range contains 206651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 206751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 206851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 206951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 207051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 207151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 207251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 207351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 207451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 207551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 207651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 207751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 207851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 207951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 208051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 208151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 208251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 208351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 208451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 208551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 208651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 208751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 208851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 208951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 209051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(char[] a, int fromIndex, int toIndex, 209151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char key) { 209251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 209351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 209451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 209551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 209651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 209751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(char[] a, int fromIndex, int toIndex, 209851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char key) { 209951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 210051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 210151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 210251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 210351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 210451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char midVal = a[mid]; 210551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 210651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 210751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 210851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 210951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 211051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 211151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 211251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 211351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 211451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 211551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 211651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 211751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of bytes for the specified value using the 211851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. The array must be sorted (as 211951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(byte[])} method) prior to making this call. If it 212051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the array contains 212151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 212251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 212351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 212451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 212551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 212651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 212751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 212851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 212951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 213051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 213151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 213251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 213351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 213451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 213551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(byte[] a, byte key) { 213651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 213751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 213851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 213951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 214051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 214151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of bytes for the specified value using the 214251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. 214351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted (as 214451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(byte[], int, int)} method) 214551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. If it 214651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the range contains 214751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 214851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 214951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 215051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 215151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 215251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 215351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 215451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 215551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 215651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 215751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 215851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 215951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 216051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 216151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 216251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 216351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 216451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 216551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 216651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 216751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 216851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 216951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 217051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 217151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(byte[] a, int fromIndex, int toIndex, 217251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski byte key) { 217351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 217451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 217551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 217651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 217751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 217851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(byte[] a, int fromIndex, int toIndex, 217951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski byte key) { 218051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 218151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 218251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 218351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 218451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 218551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski byte midVal = a[mid]; 218651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 218751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 218851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 218951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 219051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 219151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 219251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 219351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 219451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 219551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 219651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 219751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 219851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of doubles for the specified value using 219951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary search algorithm. The array must be sorted 220051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (as by the {@link #sort(double[])} method) prior to making this call. 220151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If it is not sorted, the results are undefined. If the array contains 220251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 220351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. This method considers all NaN values to be 220451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equivalent and equal. 220551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 220651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 220751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 220851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 220951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 221051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 221151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 221251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 221351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 221451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 221551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 221651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 221751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(double[] a, double key) { 221851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 221951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 222051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 222151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 222251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 222351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of doubles for the specified value using 222451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary search algorithm. 222551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted 222651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (as by the {@link #sort(double[], int, int)} method) 222751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. 222851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If it is not sorted, the results are undefined. If the range contains 222951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 223051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. This method considers all NaN values to be 223151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equivalent and equal. 223251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 223351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 223451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 223551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 223651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 223751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 223851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 223951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 224051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 224151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 224251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 224351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 224451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 224551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 224651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 224751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 224851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 224951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 225051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 225151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 225251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 225351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 225451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(double[] a, int fromIndex, int toIndex, 225551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski double key) { 225651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 225751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 225851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 225951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 226051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 226151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(double[] a, int fromIndex, int toIndex, 226251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski double key) { 226351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 226451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 226551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 226651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 226751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 226851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski double midVal = a[mid]; 226951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 227051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 227151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; // Neither val is NaN, thisVal is smaller 227251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 227351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; // Neither val is NaN, thisVal is larger 227451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else { 227551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long midBits = Double.doubleToLongBits(midVal); 227651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long keyBits = Double.doubleToLongBits(key); 227751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midBits == keyBits) // Values are equal 227851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // Key found 227951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN) 228051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 228151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else // (0.0, -0.0) or (NaN, !NaN) 228251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 228351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 228451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 228551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 228651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 228751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 228851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 228951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of floats for the specified value using 229051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary search algorithm. The array must be sorted 229151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (as by the {@link #sort(float[])} method) prior to making this call. If 229251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is not sorted, the results are undefined. If the array contains 229351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 229451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. This method considers all NaN values to be 229551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equivalent and equal. 229651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 229751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 229851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 229951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 230051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 230151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 230251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 230351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 230451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 230551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 230651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 230751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 230851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(float[] a, float key) { 230951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 231051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 231151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 231251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 231351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 231451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of floats for the specified value using 231551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary search algorithm. 231651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted 231751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (as by the {@link #sort(float[], int, int)} method) 231851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. If 231951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is not sorted, the results are undefined. If the range contains 232051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 232151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. This method considers all NaN values to be 232251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equivalent and equal. 232351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 232451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 232551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 232651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 232751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 232851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 232951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 233051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 233151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 233251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 233351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 233451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 233551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 233651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 233751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 233851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 233951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 234051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 234151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 234251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 234351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 234451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 234551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(float[] a, int fromIndex, int toIndex, 234651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski float key) { 234751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 234851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 234951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 235051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 235151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 235251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(float[] a, int fromIndex, int toIndex, 235351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski float key) { 235451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 235551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 235651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 235751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 235851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 235951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski float midVal = a[mid]; 236051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 236151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 236251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; // Neither val is NaN, thisVal is smaller 236351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 236451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; // Neither val is NaN, thisVal is larger 236551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else { 236651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int midBits = Float.floatToIntBits(midVal); 236751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int keyBits = Float.floatToIntBits(key); 236851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midBits == keyBits) // Values are equal 236951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // Key found 237051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN) 237151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 237251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else // (0.0, -0.0) or (NaN, !NaN) 237351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 237451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 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 238351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} 238451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of its elements (as by the 238551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #sort(Object[])} method) prior to making this call. 238651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If it is not sorted, the results are undefined. 238751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (If the array contains elements that are not mutually comparable (for 238851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * example, strings and integers), it <i>cannot</i> be sorted according 238951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the natural ordering of its elements, hence results are undefined.) 239051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the array contains multiple 239151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements equal to the specified object, there is no guarantee which 239251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 239351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 239451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 239551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 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 >= 0 if 240351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 240451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the search key is not comparable to the 240551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements of the array. 240651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 240751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(Object[] a, Object key) { 240851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 240951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 241051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 241151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 241251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 241351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array for the specified object using the binary 241451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * search algorithm. 241551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted into ascending order 241651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * according to the 241751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} 241851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of its elements (as by the 241951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #sort(Object[], int, int)} method) prior to making this 242051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * call. If it is not sorted, the results are undefined. 242151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (If the range contains elements that are not mutually comparable (for 242251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * example, strings and integers), it <i>cannot</i> be sorted according 242351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the natural ordering of its elements, hence results are undefined.) 242451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the range contains multiple 242551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements equal to the specified object, there is no guarantee which 242651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 242751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 242851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 242951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 243051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 243151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 243251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 243351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 243451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 243551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 243651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 243751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 243851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 243951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 244051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 244151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 244251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 244351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the search key is not comparable to the 244451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements of the array within the specified range. 244551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 244651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 244751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 244851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 244951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 245051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 245151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(Object[] a, int fromIndex, int toIndex, 245251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object key) { 245351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 245451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 245551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 245651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 245751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 245851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(Object[] a, int fromIndex, int toIndex, 245951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object key) { 246051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 246151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 246251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 246351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 246451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 246551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Comparable midVal = (Comparable)a[mid]; 246651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int cmp = midVal.compareTo(key); 246751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 246851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cmp < 0) 246951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 247051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (cmp > 0) 247151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 247251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 247351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 247451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 247551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 247651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 247751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 247851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 247951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array for the specified object using the binary 248051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * search algorithm. The array must be sorted into ascending order 248151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * according to the specified comparator (as by the 248251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #sort(Object[], Comparator) sort(T[], Comparator)} 248351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method) prior to making this call. If it is 248451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not sorted, the results are undefined. 248551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the array contains multiple 248651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements equal to the specified object, there is no guarantee which one 248751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will be found. 248851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 248951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 249051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 249151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param c the comparator by which the array is ordered. A 249251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>null</tt> value indicates that the elements' 249351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} should be used. 249451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 249551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 249651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 249751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 249851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 249951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 250051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 250151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 250251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the array contains elements that are not 250351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> using the specified comparator, 250451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or the search key is not comparable to the 250551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements of the array using this comparator. 250651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 250751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) { 250851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key, c); 250951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 251051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 251151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 251251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 251351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array for the specified object using the binary 251451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * search algorithm. 251551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted into ascending order 251651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * according to the specified comparator (as by the 251751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #sort(Object[], int, int, Comparator) 251851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sort(T[], int, int, Comparator)} 251951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method) prior to making this call. 252051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If it is not sorted, the results are undefined. 252151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the range contains multiple elements equal to the specified object, 252251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * there is no guarantee which one will be found. 252351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 252451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 252551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 252651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 252751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 252851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 252951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param c the comparator by which the array is ordered. A 253051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>null</tt> value indicates that the elements' 253151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} should be used. 253251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 253351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 253451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 253551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 253651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 253751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 253851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 253951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 254051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 254151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 254251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the range contains elements that are not 254351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> using the specified comparator, 254451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or the search key is not comparable to the 254551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range using this comparator. 254651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 254751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 254851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 254951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 255051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 255151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 255251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> int binarySearch(T[] a, int fromIndex, int toIndex, 255351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T key, Comparator<? super T> c) { 255451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 255551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key, c); 255651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 255751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 255851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 255951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex, 256051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T key, Comparator<? super T> c) { 256151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (c == null) { 256251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 256351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 256451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 256551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 256651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 256751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 256851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 256951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T midVal = a[mid]; 257051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int cmp = c.compare(midVal, key); 257151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cmp < 0) 257251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 257351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (cmp > 0) 257451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 257551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 257651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 257751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 257851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 257951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 258051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 258151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Equality Testing 258251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 258351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 258451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of longs are 258551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 258651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 258751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 258851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 258951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 259051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 259151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 259251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 259351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 259451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 259551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(long[] a, long[] a2) { 259651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 259751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 259851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 259951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 260051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 260151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 260251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 260351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 260451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 260551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 260651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i] != a2[i]) 260751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 260851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 260951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 261051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 261151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 261251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 261351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of ints are 261451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 261551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 261651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 261751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 261851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 261951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 262051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 262151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 262251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 262351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 262451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(int[] a, int[] a2) { 262551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 262651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 262751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 262851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 262951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 263051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 263151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 263251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 263351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 263451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 263551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i] != a2[i]) 263651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 263751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 263851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 263951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 264051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 264151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 264251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of shorts are 264351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 264451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 264551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 264651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 264751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 264851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 264951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 265051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 265151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 265251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 265351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(short[] a, short a2[]) { 265451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 265551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 265651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 265751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 265851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 265951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 266051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 266151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 266251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 266351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 266451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i] != a2[i]) 266551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 266651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 266751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 266851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 266951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 267051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 267151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of chars are 267251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 267351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 267451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 267551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 267651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 267751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 267851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 267951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 268051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 268151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 268251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(char[] a, char[] a2) { 268351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 268451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 268551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 268651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 268751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 268851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 268951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 269051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 269151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 269251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 269351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i] != a2[i]) 269451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 269551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 269651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 269751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 269851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 269951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 270051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of bytes are 270151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 270251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 270351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 270451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 270551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 270651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 270751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 270851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 270951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 271051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 271151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(byte[] a, byte[] a2) { 271251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 271351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 271451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 271551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 271651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 271751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 271851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 271951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 272051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 272151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 272251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i] != a2[i]) 272351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 272451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 272551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 272651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 272751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 272851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 272951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of booleans are 273051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 273151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 273251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 273351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 273451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 273551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 273651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 273751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 273851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 273951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 274051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(boolean[] a, boolean[] a2) { 274151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 274251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 274351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 274451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 274551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 274651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 274751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 274851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 274951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 275051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 275151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i] != a2[i]) 275251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 275351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 275451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 275551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 275651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 275751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 275851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of doubles are 275951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 276051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 276151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 276251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 276351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 276451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 276551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if: 276651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <pre> <tt>new Double(d1).equals(new Double(d2))</tt></pre> 276751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (Unlike the <tt>==</tt> operator, this method considers 276851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.) 276951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 277051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 277151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 277251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 277351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Double#equals(Object) 277451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 277551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(double[] a, double[] a2) { 277651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 277751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 277851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 277951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 278051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 278151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 278251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 278351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 278451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 278551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 278651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i])) 278751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 278851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 278951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 279051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 279151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 279251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 279351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of floats are 279451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 279551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 279651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 279751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 279851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 279951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 280051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Two floats <tt>f1</tt> and <tt>f2</tt> are considered equal if: 280151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <pre> <tt>new Float(f1).equals(new Float(f2))</tt></pre> 280251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (Unlike the <tt>==</tt> operator, this method considers 280351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>NaN</tt> equals to itself, and 0.0f unequal to -0.0f.) 280451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 280551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 280651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 280751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 280851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Float#equals(Object) 280951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 281051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(float[] a, float[] a2) { 281151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 281251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 281351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 281451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 281551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 281651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 281751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 281851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 281951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 282051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 282151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i])) 282251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 282351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 282451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 282551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 282651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 282751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 282851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of Objects are 282951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. The two arrays are considered equal if 283051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * both arrays contain the same number of elements, and all corresponding 283151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * pairs of elements in the two arrays are equal. Two objects <tt>e1</tt> 283251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null 283351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * : e1.equals(e2))</tt>. In other words, the two arrays are equal if 283451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * they contain the same elements in the same order. Also, two array 283551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * references are considered equal if both are <tt>null</tt>.<p> 283651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 283751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 283851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 283951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 284051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 284151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(Object[] a, Object[] a2) { 284251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 284351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 284451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 284551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 284651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 284751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 284851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 284951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 285051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 285151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) { 285251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object o1 = a[i]; 285351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object o2 = a2[i]; 285451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (!(o1==null ? o2==null : o1.equals(o2))) 285551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 285651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 285751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 285851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 285951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 286051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 286151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Filling 286251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 286351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 286451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified long value to each element of the specified array 286551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of longs. 286651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 286751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 286851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 286951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 287051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(long[] a, long val) { 287151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 287251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 287351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 287451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 287551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 287651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified long value to each element of the specified 287751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of longs. The range to be filled 287851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 287951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 288051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 288151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 288251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 288351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 288451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 288551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 288651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 288751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 288851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 288951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 289051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 289151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 289251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(long[] a, int fromIndex, int toIndex, long val) { 289351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 289451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 289551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 289651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 289751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 289851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 289951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified int value to each element of the specified array 290051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of ints. 290151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 290251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 290351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 290451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 290551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(int[] a, int val) { 290651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 290751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 290851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 290951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 291051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 291151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified int value to each element of the specified 291251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of ints. The range to be filled 291351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 291451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 291551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 291651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 291751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 291851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 291951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 292051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 292151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 292251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 292351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 292451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 292551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 292651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 292751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(int[] a, int fromIndex, int toIndex, int val) { 292851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 292951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 293051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 293151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 293251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 293351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 293451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified short value to each element of the specified array 293551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of shorts. 293651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 293751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 293851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 293951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 294051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(short[] a, short val) { 294151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 294251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 294351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 294451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 294551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 294651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified short value to each element of the specified 294751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of shorts. The range to be filled 294851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 294951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 295051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 295151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 295251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 295351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 295451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 295551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 295651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 295751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 295851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 295951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 296051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 296151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 296251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(short[] a, int fromIndex, int toIndex, short val) { 296351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 296451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 296551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 296651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 296751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 296851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 296951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified char value to each element of the specified array 297051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of chars. 297151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 297251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 297351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 297451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 297551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(char[] a, char val) { 297651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 297751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 297851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 297951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 298051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 298151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified char value to each element of the specified 298251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of chars. The range to be filled 298351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 298451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 298551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 298651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 298751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 298851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 298951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 299051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 299151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 299251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 299351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 299451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 299551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 299651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 299751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(char[] a, int fromIndex, int toIndex, char val) { 299851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 299951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 300051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 300151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 300251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 300351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 300451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified byte value to each element of the specified array 300551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of bytes. 300651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 300751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 300851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 300951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 301051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(byte[] a, byte val) { 301151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 301251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 301351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 301451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 301551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 301651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified byte value to each element of the specified 301751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of bytes. The range to be filled 301851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 301951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 302051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 302151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 302251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 302351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 302451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 302551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 302651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 302751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 302851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 302951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 303051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 303151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 303251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(byte[] a, int fromIndex, int toIndex, byte val) { 303351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 303451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 303551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 303651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 303751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 303851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 303951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified boolean value to each element of the specified 304051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of booleans. 304151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 304251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 304351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 304451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 304551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(boolean[] a, boolean val) { 304651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 304751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 304851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 304951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 305051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 305151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified boolean value to each element of the specified 305251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of booleans. The range to be filled 305351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 305451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 305551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 305651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 305751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 305851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 305951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 306051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 306151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 306251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 306351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 306451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 306551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 306651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 306751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(boolean[] a, int fromIndex, int toIndex, 306851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski boolean val) { 306951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 307051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 307151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 307251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 307351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 307451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 307551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified double value to each element of the specified 307651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of doubles. 307751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 307851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 307951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 308051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 308151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(double[] a, double val) { 308251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 308351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 308451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 308551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 308651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 308751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified double value to each element of the specified 308851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of doubles. The range to be filled 308951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 309051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 309151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 309251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 309351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 309451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 309551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 309651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 309751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 309851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 309951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 310051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 310151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 310251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 310351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(double[] a, int fromIndex, int toIndex,double val){ 310451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 310551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 310651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 310751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 310851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 310951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 311051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified float value to each element of the specified array 311151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of floats. 311251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 311351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 311451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 311551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 311651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(float[] a, float val) { 311751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 311851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 311951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 312051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 312151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 312251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified float value to each element of the specified 312351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of floats. The range to be filled 312451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 312551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 312651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 312751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 312851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 312951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 313051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 313151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 313251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 313351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 313451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 313551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 313651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 313751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 313851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(float[] a, int fromIndex, int toIndex, float val) { 313951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 314051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 314151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 314251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 314351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 314451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 314551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified Object reference to each element of the specified 314651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of Objects. 314751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 314851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 314951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 315051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayStoreException if the specified value is not of a 315151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * runtime type that can be stored in the specified array 315251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 315351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(Object[] a, Object val) { 315451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 315551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 315651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 315751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 315851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 315951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified Object reference to each element of the specified 316051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of Objects. The range to be filled 316151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 316251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 316351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 316451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 316551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 316651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 316751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 316851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 316951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 317051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 317151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 317251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 317351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 317451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayStoreException if the specified value is not of a 317551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * runtime type that can be stored in the specified array 317651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 317751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(Object[] a, int fromIndex, int toIndex, Object val) { 317851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 317951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 318051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 318151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 318251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 318351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Cloning 318451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 318551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 318651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with nulls (if necessary) 318751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 318851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 318951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 319051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>null</tt>. 319151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 319251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 319351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The resulting array is of exactly the same class as the original array. 319451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 319551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 319651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 319751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with nulls 319851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 319951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 320051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 320151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 320251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 320351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> T[] copyOf(T[] original, int newLength) { 320451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (T[]) copyOf(original, newLength, original.getClass()); 320551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 320651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 320751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 320851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with nulls (if necessary) 320951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 321051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 321151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 321251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>null</tt>. 321351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 321451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 321551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The resulting array is of the class <tt>newType</tt>. 321651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 321751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 321851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 321951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newType the class of the copy to be returned 322051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with nulls 322151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 322251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 322351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 322451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayStoreException if an element copied from 322551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original</tt> is not of a runtime type that can be stored in 322651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array of class <tt>newType</tt> 322751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 322851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 322951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { 323051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] copy = ((Object)newType == (Object)Object[].class) 323151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ? (T[]) new Object[newLength] 323251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski : (T[]) Array.newInstance(newType.getComponentType(), newLength); 323351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 323451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 323551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 323651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 323751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 323851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 323951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with zeros (if necessary) 324051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 324151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 324251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 324351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>(byte)0</tt>. 324451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 324551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 324651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 324751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 324851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 324951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with zeros 325051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 325151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 325251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 325351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 325451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 325551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static byte[] copyOf(byte[] original, int newLength) { 325651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski byte[] copy = new byte[newLength]; 325751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 325851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 325951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 326051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 326151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 326251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 326351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with zeros (if necessary) 326451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 326551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 326651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 326751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>(short)0</tt>. 326851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 326951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 327051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 327151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 327251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 327351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with zeros 327451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 327551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 327651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 327751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 327851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 327951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static short[] copyOf(short[] original, int newLength) { 328051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski short[] copy = new short[newLength]; 328151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 328251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 328351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 328451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 328551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 328651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 328751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with zeros (if necessary) 328851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 328951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 329051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 329151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>0</tt>. 329251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 329351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 329451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 329551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 329651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 329751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with zeros 329851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 329951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 330051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 330151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 330251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 330351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int[] copyOf(int[] original, int newLength) { 330451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int[] copy = new int[newLength]; 330551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 330651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 330751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 330851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 330951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 331051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 331151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with zeros (if necessary) 331251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 331351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 331451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 331551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>0L</tt>. 331651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 331751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 331851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 331951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 332051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 332151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with zeros 332251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 332351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 332451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 332551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 332651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 332751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static long[] copyOf(long[] original, int newLength) { 332851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long[] copy = new long[newLength]; 332951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 333051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 333151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 333251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 333351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 333451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 333551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with null characters (if necessary) 333651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are valid 333751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in both the original array and the copy, the two arrays will contain 333851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * identical values. For any indices that are valid in the copy but not 333951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the original, the copy will contain <tt>'\\u000'</tt>. Such indices 334051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will exist if and only if the specified length is greater than that of 334151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the original array. 334251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 334351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 334451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 334551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with null characters 334651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 334751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 334851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 334951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 335051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 335151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static char[] copyOf(char[] original, int newLength) { 335251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char[] copy = new char[newLength]; 335351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 335451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 335551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 335651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 335751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 335851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 335951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with zeros (if necessary) 336051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 336151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 336251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 336351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>0f</tt>. 336451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 336551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 336651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 336751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 336851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 336951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with zeros 337051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 337151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 337251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 337351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 337451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 337551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static float[] copyOf(float[] original, int newLength) { 337651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski float[] copy = new float[newLength]; 337751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 337851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 337951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 338051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 338151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 338251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 338351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with zeros (if necessary) 338451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 338551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 338651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 338751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>0d</tt>. 338851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 338951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 339051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 339151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 339251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 339351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with zeros 339451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 339551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 339651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 339751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 339851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 339951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static double[] copyOf(double[] original, int newLength) { 340051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski double[] copy = new double[newLength]; 340151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 340251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 340351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 340451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 340551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 340651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 340751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with <tt>false</tt> (if necessary) 340851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 340951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 341051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 341151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>false</tt>. 341251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 341351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 341451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 341551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 341651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 341751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with false elements 341851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 341951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 342051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 342151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 342251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 342351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean[] copyOf(boolean[] original, int newLength) { 342451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski boolean[] copy = new boolean[newLength]; 342551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 342651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 342751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 342851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 342951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 343051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 343151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 343251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 343351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 343451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 343551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 343651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 343751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 343851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 343951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 344051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>null</tt> is placed in all elements of the copy whose index is 344151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 344251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 344351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 344451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The resulting array is of exactly the same class as the original array. 344551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 344651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 344751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 344851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 344951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 345051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 345151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with nulls to obtain the required length 345251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 345351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 345451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 345551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 345651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 345751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 345851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> T[] copyOfRange(T[] original, int from, int to) { 345951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copyOfRange(original, from, to, (Class<T[]>) original.getClass()); 346051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 346151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 346251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 346351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 346451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 346551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 346651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 346751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 346851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 346951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 347051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 347151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 347251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>null</tt> is placed in all elements of the copy whose index is 347351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 347451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 347551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The resulting array is of the class <tt>newType</tt>. 347651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 347751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 347851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 347951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 348051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 348151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newType the class of the copy to be returned 348251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 348351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with nulls to obtain the required length 348451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 348551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 348651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 348751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 348851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayStoreException if an element copied from 348951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original</tt> is not of a runtime type that can be stored in 349051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array of class <tt>newType</tt>. 349151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 349251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 349351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) { 349451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 349551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 349651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 349751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] copy = ((Object)newType == (Object)Object[].class) 349851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ? (T[]) new Object[newLength] 349951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski : (T[]) Array.newInstance(newType.getComponentType(), newLength); 350051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 350151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 350251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 350351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 350451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 350551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 350651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 350751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 350851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 350951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 351051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 351151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 351251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 351351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 351451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 351551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>(byte)0</tt> is placed in all elements of the copy whose index is 351651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 351751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 351851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 351951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 352051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 352151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 352251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 352351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 352451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with zeros to obtain the required length 352551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 352651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 352751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 352851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 352951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 353051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 353151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static byte[] copyOfRange(byte[] original, int from, int to) { 353251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 353351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 353451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 353551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski byte[] copy = new byte[newLength]; 353651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 353751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 353851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 353951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 354051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 354151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 354251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 354351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 354451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 354551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 354651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 354751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 354851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 354951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 355051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 355151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>(short)0</tt> is placed in all elements of the copy whose index is 355251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 355351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 355451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 355551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 355651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 355751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 355851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 355951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 356051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with zeros to obtain the required length 356151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 356251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 356351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 356451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 356551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 356651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 356751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static short[] copyOfRange(short[] original, int from, int to) { 356851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 356951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 357051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 357151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski short[] copy = new short[newLength]; 357251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 357351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 357451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 357551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 357651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 357751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 357851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 357951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 358051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 358151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 358251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 358351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 358451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 358551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 358651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 358751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>0</tt> is placed in all elements of the copy whose index is 358851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 358951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 359051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 359151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 359251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 359351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 359451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 359551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 359651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with zeros to obtain the required length 359751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 359851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 359951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 360051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 360151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 360251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 360351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int[] copyOfRange(int[] original, int from, int to) { 360451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 360551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 360651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 360751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int[] copy = new int[newLength]; 360851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 360951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 361051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 361151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 361251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 361351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 361451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 361551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 361651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 361751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 361851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 361951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 362051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 362151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 362251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 362351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>0L</tt> is placed in all elements of the copy whose index is 362451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 362551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 362651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 362751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 362851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 362951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 363051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 363151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 363251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with zeros to obtain the required length 363351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 363451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 363551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 363651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 363751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 363851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 363951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static long[] copyOfRange(long[] original, int from, int to) { 364051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 364151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 364251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 364351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long[] copy = new long[newLength]; 364451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 364551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 364651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 364751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 364851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 364951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 365051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 365151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 365251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 365351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 365451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 365551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 365651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 365751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 365851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 365951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>'\\u000'</tt> is placed in all elements of the copy whose index is 366051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 366151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 366251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 366351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 366451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 366551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 366651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 366751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 366851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with null characters to obtain the required length 366951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 367051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 367151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 367251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 367351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 367451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 367551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static char[] copyOfRange(char[] original, int from, int to) { 367651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 367751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 367851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 367951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char[] copy = new char[newLength]; 368051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 368151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 368251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 368351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 368451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 368551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 368651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 368751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 368851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 368951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 369051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 369151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 369251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 369351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 369451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 369551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>0f</tt> is placed in all elements of the copy whose index is 369651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 369751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 369851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 369951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 370051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 370151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 370251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 370351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 370451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with zeros to obtain the required length 370551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 370651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 370751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 370851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 370951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 371051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 371151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static float[] copyOfRange(float[] original, int from, int to) { 371251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 371351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 371451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 371551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski float[] copy = new float[newLength]; 371651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 371751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 371851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 371951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 372051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 372151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 372251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 372351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 372451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 372551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 372651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 372751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 372851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 372951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 373051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 373151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>0d</tt> is placed in all elements of the copy whose index is 373251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 373351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 373451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 373551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 373651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 373751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 373851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 373951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 374051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with zeros to obtain the required length 374151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 374251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 374351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 374451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 374551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 374651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 374751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static double[] copyOfRange(double[] original, int from, int to) { 374851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 374951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 375051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 375151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski double[] copy = new double[newLength]; 375251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 375351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 375451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 375551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 375651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 375751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 375851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 375951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 376051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 376151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 376251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 376351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 376451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 376551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 376651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 376751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>false</tt> is placed in all elements of the copy whose index is 376851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 376951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 377051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 377151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 377251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 377351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 377451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 377551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 377651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with false elements to obtain the required length 377751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 377851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 377951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 378051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 378151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 378251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 378351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean[] copyOfRange(boolean[] original, int from, int to) { 378451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 378551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 378651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 378751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski boolean[] copy = new boolean[newLength]; 378851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 378951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 379051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 379151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 379251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 379351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Misc 379451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 379551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 379651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a fixed-size list backed by the specified array. (Changes to 379751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the returned list "write through" to the array.) This method acts 379851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as bridge between array-based and collection-based APIs, in 379951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * combination with {@link Collection#toArray}. The returned list is 380051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * serializable and implements {@link RandomAccess}. 380151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 380251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This method also provides a convenient way to create a fixed-size 380351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * list initialized to contain several elements: 380451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <pre> 380551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * List<String> stooges = Arrays.asList("Larry", "Moe", "Curly"); 380651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre> 380751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 380851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array by which the list will be backed 380951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a list view of the specified array 381051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 381151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @SafeVarargs 381251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> List<T> asList(T... a) { 381351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return new ArrayList<>(a); 381451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 381551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 381651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 381751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @serial include 381851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 381951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static class ArrayList<E> extends AbstractList<E> 382051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski implements RandomAccess, java.io.Serializable 382151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski { 382251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static final long serialVersionUID = -2764017481108945198L; 382351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private final E[] a; 382451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 382551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ArrayList(E[] array) { 38264c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath a = Objects.requireNonNull(array); 382751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 382851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 38294c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 383051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int size() { 383151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return a.length; 383251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 383351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 38344c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 383551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Object[] toArray() { 383651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return a.clone(); 383751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 383851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 38394c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 38404c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @SuppressWarnings("unchecked") 384151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public <T> T[] toArray(T[] a) { 384251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int size = size(); 384351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a.length < size) 384451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return Arrays.copyOf(this.a, size, 384551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski (Class<? extends T[]>) a.getClass()); 384651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(this.a, 0, a, 0, size); 384751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a.length > size) 384851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[size] = null; 384951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return a; 385051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 385151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 38524c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 385351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public E get(int index) { 385451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return a[index]; 385551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 385651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 38574c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 385851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public E set(int index, E element) { 385951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski E oldValue = a[index]; 386051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[index] = element; 386151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return oldValue; 386251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 386351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 38644c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 386551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int indexOf(Object o) { 386651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (o==null) { 386751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<a.length; i++) 386851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i]==null) 386951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return i; 387051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 387151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<a.length; i++) 387251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (o.equals(a[i])) 387351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return i; 387451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 387551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -1; 387651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 387751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 38784c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 387951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean contains(Object o) { 388051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return indexOf(o) != -1; 388151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 388202f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak 388302f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak @Override 388402f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak public void forEach(Consumer<? super E> action) { 388502f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak Objects.requireNonNull(action); 388602f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak for (E e : a) { 388702f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak action.accept(e); 388802f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak } 388902f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak } 38904c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 38914c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 38922c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera public void replaceAll(UnaryOperator<E> operator) { 38932c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera Objects.requireNonNull(operator); 38942c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera E[] a = this.a; 38952c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera for (int i = 0; i < a.length; i++) { 38962c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera a[i] = operator.apply(a[i]); 38972c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera } 38982c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera } 38992c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera 39002c5ded18d09c06540892bfee5d20b69038029f8aShubham Ajmera @Override 39014c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public Spliterator<E> spliterator() { 39024c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(a, Spliterator.ORDERED); 39034c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 390451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 390551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 390651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 390751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 390851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>long</tt> arrays <tt>a</tt> and <tt>b</tt> 390951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 391051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 391151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 391251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 391351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 391451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Long} 391551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 391651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 391751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 391851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 391951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 392051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 392151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 392251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(long a[]) { 392351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 392451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 392551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 392651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 392751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (long element : a) { 392851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int elementHash = (int)(element ^ (element >>> 32)); 392951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + elementHash; 393051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 393151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 393251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 393351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 393451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 393551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 393651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 393751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two non-null <tt>int</tt> arrays <tt>a</tt> and <tt>b</tt> 393851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 393951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 394051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 394151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 394251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 394351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Integer} 394451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 394551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 394651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 394751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 394851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 394951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 395051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 395151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(int a[]) { 395251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 395351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 395451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 395551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 395651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int element : a) 395751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + element; 395851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 395951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 396051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 396151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 396251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 396351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 396451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>short</tt> arrays <tt>a</tt> and <tt>b</tt> 396551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 396651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 396751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 396851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 396951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 397051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Short} 397151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 397251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 397351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 397451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 397551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 397651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 397751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 397851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(short a[]) { 397951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 398051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 398151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 398251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 398351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (short element : a) 398451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + element; 398551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 398651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 398751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 398851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 398951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 399051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 399151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>char</tt> arrays <tt>a</tt> and <tt>b</tt> 399251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 399351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 399451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 399551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 399651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 399751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Character} 399851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 399951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 400051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 400151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 400251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 400351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 400451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 400551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(char a[]) { 400651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 400751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 400851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 400951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 401051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (char element : a) 401151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + element; 401251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 401351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 401451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 401551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 401651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 401751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 401851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>byte</tt> arrays <tt>a</tt> and <tt>b</tt> 401951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 402051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 402151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 402251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 402351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 402451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Byte} 402551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 402651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 402751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 402851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 402951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 403051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 403151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 403251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(byte a[]) { 403351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 403451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 403551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 403651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 403751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (byte element : a) 403851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + element; 403951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 404051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 404151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 404251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 404351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 404451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 404551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>boolean</tt> arrays <tt>a</tt> and <tt>b</tt> 404651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 404751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 404851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 404951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 405051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 405151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Boolean} 405251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 405351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 405451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 405551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 405651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 405751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 405851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 405951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(boolean a[]) { 406051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 406151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 406251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 406351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 406451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (boolean element : a) 406551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + (element ? 1231 : 1237); 406651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 406751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 406851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 406951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 407051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 407151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 407251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>float</tt> arrays <tt>a</tt> and <tt>b</tt> 407351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 407451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 407551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 407651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 407751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 407851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Float} 407951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 408051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 408151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 408251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 408351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 408451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 408551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 408651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(float a[]) { 408751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 408851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 408951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 409051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 409151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (float element : a) 409251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + Float.floatToIntBits(element); 409351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 409451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 409551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 409651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 409751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 409851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 409951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>double</tt> arrays <tt>a</tt> and <tt>b</tt> 410051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 410151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 410251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 410351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 410451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 410551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Double} 410651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 410751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 410851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 410951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 411051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 411151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 411251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 411351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(double a[]) { 411451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 411551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 411651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 411751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 411851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (double element : a) { 411951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long bits = Double.doubleToLongBits(element); 412051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + (int)(bits ^ (bits >>> 32)); 412151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 412251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 412351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 412451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 412551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 412651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. If 412751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the array contains other arrays as elements, the hash code is based on 412851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * their identities rather than their contents. It is therefore 412951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * acceptable to invoke this method on an array that contains itself as an 413051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element, either directly or indirectly through one or more levels of 413151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays. 413251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 413351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that 413451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.equals(a, b)</tt>, it is also the case that 413551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 413651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 413751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is equal to the value that would 413851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be returned by <tt>Arrays.asList(a).hashCode()</tt>, unless <tt>a</tt> 413951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>, in which case <tt>0</tt> is returned. 414051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 414151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose content-based hash code to compute 414251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 414351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #deepHashCode(Object[]) 414451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 414551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 414651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(Object a[]) { 414751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 414851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 414951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 415051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 415151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 415251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (Object element : a) 415351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + (element == null ? 0 : element.hashCode()); 415451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 415551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 415651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 415751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 415851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 415951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the "deep contents" of the specified 416051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array. If the array contains other arrays as elements, the 416151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * hash code is based on their contents and so on, ad infinitum. 416251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * It is therefore unacceptable to invoke this method on an array that 416351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contains itself as an element, either directly or indirectly through 416451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one or more levels of arrays. The behavior of such an invocation is 416551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * undefined. 416651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 416751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that 416851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.deepEquals(a, b)</tt>, it is also the case that 416951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.deepHashCode(a) == Arrays.deepHashCode(b)</tt>. 417051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 417151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The computation of the value returned by this method is similar to 417251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that of the value returned by {@link List#hashCode()} on a list 417351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * containing the same elements as <tt>a</tt> in the same order, with one 417451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * difference: If an element <tt>e</tt> of <tt>a</tt> is itself an array, 417551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * its hash code is computed not by calling <tt>e.hashCode()</tt>, but as 417651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by calling the appropriate overloading of <tt>Arrays.hashCode(e)</tt> 417751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if <tt>e</tt> is an array of a primitive type, or as by calling 417851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.deepHashCode(e)</tt> recursively if <tt>e</tt> is an array 417951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of a reference type. If <tt>a</tt> is <tt>null</tt>, this method 418051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns 0. 418151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 418251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose deep-content-based hash code to compute 418351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a deep-content-based hash code for <tt>a</tt> 418451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #hashCode(Object[]) 418551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 418651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 418751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int deepHashCode(Object a[]) { 418851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 418951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 419051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 419151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 419251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 419351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (Object element : a) { 419451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int elementHash = 0; 4195669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak if (element != null) { 4196669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak Class<?> cl = element.getClass().getComponentType(); 4197669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak if (cl == null) 4198669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = element.hashCode(); 4199669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (element instanceof Object[]) 4200669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = deepHashCode((Object[]) element); 4201669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == byte.class) 4202669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((byte[]) element); 4203669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == short.class) 4204669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((short[]) element); 4205669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == int.class) 4206669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((int[]) element); 4207669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == long.class) 4208669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((long[]) element); 4209669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == char.class) 4210669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((char[]) element); 4211669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == float.class) 4212669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((float[]) element); 4213669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == double.class) 4214669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((double[]) element); 4215669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == boolean.class) 4216669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((boolean[]) element); 4217669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else 4218669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = element.hashCode(); 4219669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak } 422051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + elementHash; 422151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 422251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 422351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 422451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 422551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 422651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 422751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays are <i>deeply 422851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equal</i> to one another. Unlike the {@link #equals(Object[],Object[])} 422951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method, this method is appropriate for use with nested arrays of 423051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arbitrary depth. 423151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 423251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Two array references are considered deeply equal if both 423351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are <tt>null</tt>, or if they refer to arrays that contain the same 423451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * number of elements and all corresponding pairs of elements in the two 423551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays are deeply equal. 423651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 423751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Two possibly <tt>null</tt> elements <tt>e1</tt> and <tt>e2</tt> are 423851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * deeply equal if any of the following conditions hold: 423951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 424051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> <tt>e1</tt> and <tt>e2</tt> are both arrays of object reference 424151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types, and <tt>Arrays.deepEquals(e1, e2) would return true</tt> 424251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> <tt>e1</tt> and <tt>e2</tt> are arrays of the same primitive 424351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type, and the appropriate overloading of 424451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.equals(e1, e2)</tt> would return true. 424551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> <tt>e1 == e2</tt> 424651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> <tt>e1.equals(e2)</tt> would return true. 424751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 424851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this definition permits <tt>null</tt> elements at any depth. 424951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 425051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If either of the specified arrays contain themselves as elements 425151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * either directly or indirectly through one or more levels of arrays, 425251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the behavior of this method is undefined. 425351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 425451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a1 one array to be tested for equality 425551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 425651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 425751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #equals(Object[],Object[]) 425851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Objects#deepEquals(Object, Object) 425951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 426051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 426151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean deepEquals(Object[] a1, Object[] a2) { 426251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a1 == a2) 426351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 426451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a1 == null || a2==null) 426551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 426651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a1.length; 426751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 426851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 426951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 427051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; i < length; i++) { 427151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object e1 = a1[i]; 427251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object e2 = a2[i]; 427351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 427451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (e1 == e2) 427551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski continue; 4276669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak if (e1 == null || e2 == null) 427751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 427851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 427951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Figure out whether the two elements are equal 428051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski boolean eq = deepEquals0(e1, e2); 428151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 428251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (!eq) 428351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 428451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 428551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 428651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 428751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 428851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static boolean deepEquals0(Object e1, Object e2) { 4289669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak Class<?> cl1 = e1.getClass().getComponentType(); 4290669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak Class<?> cl2 = e2.getClass().getComponentType(); 4291669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak 4292669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak if (cl1 != cl2) { 4293669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return false; 4294669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak } 4295669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak if (e1 instanceof Object[]) 4296669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return deepEquals ((Object[]) e1, (Object[]) e2); 4297669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == byte.class) 4298669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((byte[]) e1, (byte[]) e2); 4299669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == short.class) 4300669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((short[]) e1, (short[]) e2); 4301669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == int.class) 4302669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((int[]) e1, (int[]) e2); 4303669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == long.class) 4304669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((long[]) e1, (long[]) e2); 4305669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == char.class) 4306669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((char[]) e1, (char[]) e2); 4307669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == float.class) 4308669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((float[]) e1, (float[]) e2); 4309669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == double.class) 4310669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((double[]) e1, (double[]) e2); 4311669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == boolean.class) 4312669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((boolean[]) e1, (boolean[]) e2); 431351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 4314669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return e1.equals(e2); 431551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 431651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 431751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 431851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 431951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 432051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 432151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 432251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 432351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(long)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 432451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>. 432551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 432651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 432751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 432851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 432951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 433051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(long[] a) { 433151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 433251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 433351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 433451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 433551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 433651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 433751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 433851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 433951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 434051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 434151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 434251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 434351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 434451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 434551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 434651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 434751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 434851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 434951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 435051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 435151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 435251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 435351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(int)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> is 435451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>null</tt>. 435551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 435651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 435751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 435851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 435951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 436051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(int[] a) { 436151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 436251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 436351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 436451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 436551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 436651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 436751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 436851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 436951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 437051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 437151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 437251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 437351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 437451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 437551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 437651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 437751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 437851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 437951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 438051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 438151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 438251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 438351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(short)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 438451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>. 438551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 438651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 438751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 438851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 438951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 439051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(short[] a) { 439151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 439251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 439351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 439451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 439551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 439651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 439751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 439851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 439951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 440051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 440151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 440251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 440351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 440451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 440551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 440651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 440751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 440851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 440951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 441051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 441151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 441251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 441351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(char)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 441451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>. 441551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 441651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 441751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 441851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 441951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 442051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(char[] a) { 442151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 442251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 442351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 442451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 442551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 442651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 442751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 442851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 442951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 443051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 443151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 443251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 443351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 443451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 443551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 443651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 443751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 443851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 443951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 444051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements 444151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are separated by the characters <tt>", "</tt> (a comma followed 444251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by a space). Elements are converted to strings as by 444351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(byte)</tt>. Returns <tt>"null"</tt> if 444451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>a</tt> is <tt>null</tt>. 444551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 444651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 444751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 444851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 444951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 445051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(byte[] a) { 445151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 445251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 445351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 445451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 445551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 445651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 445751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 445851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 445951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 446051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 446151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 446251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 446351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 446451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 446551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 446651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 446751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 446851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 446951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 447051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 447151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 447251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 447351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(boolean)</tt>. Returns <tt>"null"</tt> if 447451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>a</tt> is <tt>null</tt>. 447551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 447651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 447751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 447851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 447951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 448051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(boolean[] a) { 448151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 448251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 448351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 448451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 448551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 448651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 448751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 448851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 448951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 449051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 449151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 449251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 449351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 449451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 449551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 449651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 449751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 449851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 449951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 450051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 450151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 450251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 450351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(float)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 450451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>. 450551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 450651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 450751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 450851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 450951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 451051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(float[] a) { 451151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 451251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 451351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 451451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 451551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 451651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 451751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 451851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 451951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 452051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 452151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 452251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 452351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 452451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 452551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 452651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 452751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 452851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 452951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 453051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 453151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 453251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 453351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 453451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(double)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 453551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>. 453651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 453751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 453851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 453951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 454051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 454151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(double[] a) { 454251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 454351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 454451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 454551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 454651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 454751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 454851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 454951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 455051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 455151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 455251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 455351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 455451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 455551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 455651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 455751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 455851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 455951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 456051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the array contains other arrays as elements, they are converted to 456151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * strings by the {@link Object#toString} method inherited from 456251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Object</tt>, which describes their <i>identities</i> rather than 456351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * their contents. 456451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 456551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is equal to the value that would 456651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt> 456751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>, in which case <tt>"null"</tt> is returned. 456851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 456951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 457051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 457151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #deepToString(Object[]) 457251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 457351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 457451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(Object[] a) { 457551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 457651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 457751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 457851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 457951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 458051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 458151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 458251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 458351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 458451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 458551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(String.valueOf(a[i])); 458651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 458751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 458851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 458951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 459051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 459151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 459251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 459351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the "deep contents" of the specified 459451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array. If the array contains other arrays as elements, the string 459551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representation contains their contents and so on. This method is 459651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * designed for converting multidimensional arrays to strings. 459751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 459851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The string representation consists of a list of the array's 459951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements, enclosed in square brackets (<tt>"[]"</tt>). Adjacent 460051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements are separated by the characters <tt>", "</tt> (a comma 460151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * followed by a space). Elements are converted to strings as by 460251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(Object)</tt>, unless they are themselves 460351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays. 460451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 460551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If an element <tt>e</tt> is an array of a primitive type, it is 460651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * converted to a string as by invoking the appropriate overloading of 460751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.toString(e)</tt>. If an element <tt>e</tt> is an array of a 460851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * reference type, it is converted to a string as by invoking 460951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method recursively. 461051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 461151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>To avoid infinite recursion, if the specified array contains itself 461251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as an element, or contains an indirect reference to itself through one 461351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or more levels of arrays, the self-reference is converted to the string 461451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>"[...]"</tt>. For example, an array containing only a reference 461551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to itself would be rendered as <tt>"[[...]]"</tt>. 461651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 461751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This method returns <tt>"null"</tt> if the specified array 461851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>. 461951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 462051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 462151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 462251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #toString(Object[]) 462351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 462451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 462551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String deepToString(Object[] a) { 462651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 462751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 462851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 462951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int bufLen = 20 * a.length; 463051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a.length != 0 && bufLen <= 0) 463151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski bufLen = Integer.MAX_VALUE; 463251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder buf = new StringBuilder(bufLen); 463351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski deepToString(a, buf, new HashSet<Object[]>()); 463451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return buf.toString(); 463551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 463651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 463751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void deepToString(Object[] a, StringBuilder buf, 463851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Set<Object[]> dejaVu) { 463951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) { 464051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append("null"); 464151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 464251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 464351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 464451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) { 464551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append("[]"); 464651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 464751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 464851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 464951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski dejaVu.add(a); 465051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append('['); 465151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 465251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 465351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object element = a[i]; 465451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (element == null) { 465551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append("null"); 465651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 465751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class eClass = element.getClass(); 465851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 465951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (eClass.isArray()) { 466051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (eClass == byte[].class) 466151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((byte[]) element)); 466251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == short[].class) 466351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((short[]) element)); 466451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == int[].class) 466551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((int[]) element)); 466651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == long[].class) 466751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((long[]) element)); 466851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == char[].class) 466951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((char[]) element)); 467051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == float[].class) 467151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((float[]) element)); 467251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == double[].class) 467351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((double[]) element)); 467451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == boolean[].class) 467551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((boolean[]) element)); 467651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else { // element is an array of object references 467751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (dejaVu.contains(element)) 467851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append("[...]"); 467951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 468051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski deepToString((Object[])element, buf, dejaVu); 468151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 468251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { // element is non-null and not an array 468351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(element.toString()); 468451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 468551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 468651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 468751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski break; 468851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(", "); 468951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 469051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(']'); 469151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski dejaVu.remove(a); 469251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 4693983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski 4694983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski /** 4695e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * Set all elements of the specified array, using the provided 4696e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * generator function to compute each element. 4697e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4698e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * <p>If the generator function throws an exception, it is relayed to 4699e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * the caller and the array is left in an indeterminate state. 4700e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4701e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param <T> type of elements of the array 4702e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param array array to be initialized 4703e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param generator a function accepting an index and producing the desired 4704e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * value for that position 4705e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @throws NullPointerException if the generator is null 4706e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @since 1.8 4707e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera */ 4708e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera public static <T> void setAll(T[] array, IntFunction<? extends T> generator) { 4709e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera Objects.requireNonNull(generator); 4710e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera for (int i = 0; i < array.length; i++) 4711e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera array[i] = generator.apply(i); 4712e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera } 4713e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera 4714e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera /** 4715501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * Set all elements of the specified array, in parallel, using the 4716501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * provided generator function to compute each element. 4717501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * 4718501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * <p>If the generator function throws an exception, an unchecked exception 4719501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * is thrown from {@code parallelSetAll} and the array is left in an 4720501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * indeterminate state. 4721501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * 4722501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @param <T> type of elements of the array 4723501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @param array array to be initialized 4724501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @param generator a function accepting an index and producing the desired 4725501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * value for that position 4726501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @throws NullPointerException if the generator is null 4727501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @since 1.8 4728501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera */ 4729501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) { 4730501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera Objects.requireNonNull(generator); 4731501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); }); 4732501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera } 4733501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera 4734501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera /** 4735e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * Set all elements of the specified array, using the provided 4736e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * generator function to compute each element. 4737e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4738e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * <p>If the generator function throws an exception, it is relayed to 4739e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * the caller and the array is left in an indeterminate state. 4740e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4741e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param array array to be initialized 4742e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param generator a function accepting an index and producing the desired 4743e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * value for that position 4744e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @throws NullPointerException if the generator is null 4745e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @since 1.8 4746e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera */ 4747e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera public static void setAll(int[] array, IntUnaryOperator generator) { 4748e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera Objects.requireNonNull(generator); 4749e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera for (int i = 0; i < array.length; i++) 4750e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera array[i] = generator.applyAsInt(i); 4751e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera } 4752e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera 4753e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera /** 4754501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * Set all elements of the specified array, in parallel, using the 4755501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * provided generator function to compute each element. 4756501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * 4757501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * <p>If the generator function throws an exception, an unchecked exception 4758501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * is thrown from {@code parallelSetAll} and the array is left in an 4759501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * indeterminate state. 4760501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * 4761501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @param array array to be initialized 4762501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @param generator a function accepting an index and producing the desired 4763501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * value for that position 4764501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @throws NullPointerException if the generator is null 4765501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @since 1.8 4766501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera */ 4767501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera public static void parallelSetAll(int[] array, IntUnaryOperator generator) { 4768501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera Objects.requireNonNull(generator); 4769501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); }); 4770501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera } 4771501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera 4772501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera /** 4773e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * Set all elements of the specified array, using the provided 4774e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * generator function to compute each element. 4775e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4776e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * <p>If the generator function throws an exception, it is relayed to 4777e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * the caller and the array is left in an indeterminate state. 4778e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4779e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param array array to be initialized 4780e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param generator a function accepting an index and producing the desired 4781e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * value for that position 4782e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @throws NullPointerException if the generator is null 4783e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @since 1.8 4784e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera */ 4785e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera public static void setAll(long[] array, IntToLongFunction generator) { 4786e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera Objects.requireNonNull(generator); 4787e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera for (int i = 0; i < array.length; i++) 4788e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera array[i] = generator.applyAsLong(i); 4789e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera } 4790e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera 4791e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera /** 4792501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * Set all elements of the specified array, in parallel, using the 4793501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * provided generator function to compute each element. 4794501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * 4795501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * <p>If the generator function throws an exception, an unchecked exception 4796501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * is thrown from {@code parallelSetAll} and the array is left in an 4797501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * indeterminate state. 4798501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * 4799501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @param array array to be initialized 4800501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @param generator a function accepting an index and producing the desired 4801501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * value for that position 4802501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @throws NullPointerException if the generator is null 4803501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @since 1.8 4804501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera */ 4805501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera public static void parallelSetAll(long[] array, IntToLongFunction generator) { 4806501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera Objects.requireNonNull(generator); 4807501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); }); 4808501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera } 4809501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera 4810501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera /** 4811e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * Set all elements of the specified array, using the provided 4812e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * generator function to compute each element. 4813e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4814e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * <p>If the generator function throws an exception, it is relayed to 4815e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * the caller and the array is left in an indeterminate state. 4816e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4817e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param array array to be initialized 4818e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param generator a function accepting an index and producing the desired 4819e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * value for that position 4820e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @throws NullPointerException if the generator is null 4821e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @since 1.8 4822e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera */ 4823e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera public static void setAll(double[] array, IntToDoubleFunction generator) { 4824e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera Objects.requireNonNull(generator); 4825e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera for (int i = 0; i < array.length; i++) 4826e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera array[i] = generator.applyAsDouble(i); 4827e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera } 4828e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera 4829e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera /** 4830501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * Set all elements of the specified array, in parallel, using the 4831501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * provided generator function to compute each element. 4832501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * 4833501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * <p>If the generator function throws an exception, an unchecked exception 4834501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * is thrown from {@code parallelSetAll} and the array is left in an 4835501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * indeterminate state. 4836501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * 4837501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @param array array to be initialized 4838501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @param generator a function accepting an index and producing the desired 4839501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * value for that position 4840501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @throws NullPointerException if the generator is null 4841501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera * @since 1.8 4842501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera */ 4843501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera public static void parallelSetAll(double[] array, IntToDoubleFunction generator) { 4844501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera Objects.requireNonNull(generator); 4845501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); }); 4846501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera } 4847501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera 4848501c3a2e032cf10db12fb4641f4d3a3ebfa57a81Shubham Ajmera /** 4849983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski * Checks that the range described by {@code offset} and {@code count} doesn't exceed 4850983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski * {@code arrayLength}. 4851983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski * 4852983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski * Android changed. 4853983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski * @hide 4854983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski */ 4855983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski public static void checkOffsetAndCount(int arrayLength, int offset, int count) { 4856983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski if ((offset | count) < 0 || offset > arrayLength || arrayLength - offset < count) { 4857983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski throw new ArrayIndexOutOfBoundsException(arrayLength, offset, 4858983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski count); 4859983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski } 4860983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski } 48614c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 48624c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 48634c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 48644c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator} covering all of the specified array. 48654c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 48664c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 48674c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 48684c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 48694c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 48704c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param <T> type of elements 48714c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 48724c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 48734c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 48744c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 48754c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static <T> Spliterator<T> spliterator(T[] array) { 48764c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, 48774c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 48784c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 48794c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 48804c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 48814c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator} covering the specified range of the 48824c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * specified array. 48834c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 48844c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 48854c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 48864c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 48874c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 48884c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param <T> type of elements 48894c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 48904c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param startInclusive the first index to cover, inclusive 48914c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param endExclusive index immediately past the last index to cover 48924c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 48934c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 48944c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * negative, {@code endExclusive} is less than 48954c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@code startInclusive}, or {@code endExclusive} is greater than 48964c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * the array size 48974c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 48984c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 48994c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) { 49004c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, startInclusive, endExclusive, 49014c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 49024c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 49034c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 49044c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 49054c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator.OfInt} covering all of the specified array. 49064c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 49074c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 49084c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 49094c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 49104c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 49114c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 49124c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 49134c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 49144c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 49154c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static Spliterator.OfInt spliterator(int[] array) { 49164c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, 49174c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 49184c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 49194c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 49204c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 49214c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator.OfInt} covering the specified range of the 49224c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * specified array. 49234c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 49244c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 49254c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 49264c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 49274c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 49284c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 49294c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param startInclusive the first index to cover, inclusive 49304c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param endExclusive index immediately past the last index to cover 49314c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 49324c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 49334c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * negative, {@code endExclusive} is less than 49344c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@code startInclusive}, or {@code endExclusive} is greater than 49354c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * the array size 49364c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 49374c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 49384c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) { 49394c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, startInclusive, endExclusive, 49404c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 49414c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 49424c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 49434c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 49444c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator.OfLong} covering all of the specified array. 49454c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 49464c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 49474c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 49484c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 49494c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 49504c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 49514c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return the spliterator for the array elements 49524c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 49534c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 49544c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static Spliterator.OfLong spliterator(long[] array) { 49554c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, 49564c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 49574c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 49584c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 49594c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 49604c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator.OfLong} covering the specified range of the 49614c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * specified array. 49624c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 49634c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 49644c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 49654c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 49664c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 49674c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 49684c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param startInclusive the first index to cover, inclusive 49694c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param endExclusive index immediately past the last index to cover 49704c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 49714c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 49724c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * negative, {@code endExclusive} is less than 49734c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@code startInclusive}, or {@code endExclusive} is greater than 49744c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * the array size 49754c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 49764c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 49774c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) { 49784c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, startInclusive, endExclusive, 49794c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 49804c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 49814c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 49824c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 49834c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator.OfDouble} covering all of the specified 49844c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * array. 49854c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 49864c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 49874c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 49884c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 49894c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 49904c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 49914c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 49924c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 49934c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 49944c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static Spliterator.OfDouble spliterator(double[] array) { 49954c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, 49964c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 49974c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 49984c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 49994c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 50004c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator.OfDouble} covering the specified range of 50014c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * the specified array. 50024c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 50034c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 50044c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 50054c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 50064c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 50074c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 50084c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param startInclusive the first index to cover, inclusive 50094c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param endExclusive index immediately past the last index to cover 50104c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 50114c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 50124c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * negative, {@code endExclusive} is less than 50134c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@code startInclusive}, or {@code endExclusive} is greater than 50144c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * the array size 50154c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 50164c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 50174c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) { 50184c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, startInclusive, endExclusive, 50194c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 50204c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 5021ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 5022ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 5023ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 5024ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link Stream} with the specified array as its 5025ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * source. 5026ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 5027ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param <T> The type of the array elements 5028ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array The array, assumed to be unmodified during use 5029ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return a {@code Stream} for the array 5030ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 5031ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 5032ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static <T> Stream<T> stream(T[] array) { 5033ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return stream(array, 0, array.length); 5034ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 5035ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 5036ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 5037ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link Stream} with the specified range of the 5038ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * specified array as its source. 5039ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 5040ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param <T> the type of the array elements 5041ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 5042ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param startInclusive the first index to cover, inclusive 5043ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param endExclusive index immediately past the last index to cover 5044ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return a {@code Stream} for the array range 5045ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5046ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * negative, {@code endExclusive} is less than 5047ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * {@code startInclusive}, or {@code endExclusive} is greater than 5048ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * the array size 5049ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 5050ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 5051ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive) { 5052ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return StreamSupport.stream(spliterator(array, startInclusive, endExclusive), false); 5053ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 5054ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 5055ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 5056ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link IntStream} with the specified array as its 5057ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * source. 5058ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 5059ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 5060ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return an {@code IntStream} for the array 5061ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 5062ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 5063ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static IntStream stream(int[] array) { 5064ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return stream(array, 0, array.length); 5065ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 5066ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 5067ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 5068ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link IntStream} with the specified range of the 5069ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * specified array as its source. 5070ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 5071ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 5072ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param startInclusive the first index to cover, inclusive 5073ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param endExclusive index immediately past the last index to cover 5074ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return an {@code IntStream} for the array range 5075ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5076ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * negative, {@code endExclusive} is less than 5077ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * {@code startInclusive}, or {@code endExclusive} is greater than 5078ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * the array size 5079ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 5080ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 5081ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static IntStream stream(int[] array, int startInclusive, int endExclusive) { 5082ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return StreamSupport.intStream(spliterator(array, startInclusive, endExclusive), false); 5083ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 5084ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 5085ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 5086ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link LongStream} with the specified array as its 5087ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * source. 5088ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 5089ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 5090ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return a {@code LongStream} for the array 5091ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 5092ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 5093ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static LongStream stream(long[] array) { 5094ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return stream(array, 0, array.length); 5095ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 5096ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 5097ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 5098ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link LongStream} with the specified range of the 5099ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * specified array as its source. 5100ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 5101ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 5102ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param startInclusive the first index to cover, inclusive 5103ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param endExclusive index immediately past the last index to cover 5104ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return a {@code LongStream} for the array range 5105ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5106ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * negative, {@code endExclusive} is less than 5107ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * {@code startInclusive}, or {@code endExclusive} is greater than 5108ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * the array size 5109ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 5110ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 5111ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static LongStream stream(long[] array, int startInclusive, int endExclusive) { 5112ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return StreamSupport.longStream(spliterator(array, startInclusive, endExclusive), false); 5113ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 5114ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 5115ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 5116ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link DoubleStream} with the specified array as its 5117ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * source. 5118ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 5119ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 5120ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return a {@code DoubleStream} for the array 5121ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 5122ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 5123ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static DoubleStream stream(double[] array) { 5124ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return stream(array, 0, array.length); 5125ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 5126ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 5127ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 5128ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link DoubleStream} with the specified range of the 5129ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * specified array as its source. 5130ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 5131ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 5132ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param startInclusive the first index to cover, inclusive 5133ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param endExclusive index immediately past the last index to cover 5134ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return a {@code DoubleStream} for the array range 5135ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5136ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * negative, {@code endExclusive} is less than 5137ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * {@code startInclusive}, or {@code endExclusive} is greater than 5138ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * the array size 5139ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 5140ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 5141ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static DoubleStream stream(double[] array, int startInclusive, int endExclusive) { 5142ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false); 5143ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 514451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 5145