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