/** Sorts an array according to the natural ascending order using quicksort. * * <p>The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas * McIlroy, “Engineering a Sort Function”, <i>Software: Practice and Experience</i>, 23(11), pages * 1249−1265, 1993. * * @param x the array to be sorted. * */ public static void quickSort( final boolean[] x ) { quickSort( x, 0, x.length ); } /** Sorts the specified range of elements according to the natural ascending order using mergesort, using a given support array.
/** Sets the length of the given array. * * @param array an array. * @param length the new length for the array. * @return <code>array</code>, if it contains exactly <code>length</code> * entries; otherwise, if it contains <em>more</em> than * <code>length</code> entries, an array with <code>length</code> entries * whose entries are the same as the first <code>length</code> entries of * <code>array</code>; otherwise, an array with <code>length</code> entries * whose first <code>array.length</code> entries are the same as those of * <code>array</code>. * */ public static boolean[] setLength( final boolean[] array, final int length ) { if ( length == array.length ) return array; if ( length < array.length ) return trim( array, length ); return ensureCapacity( array, length ); } /** Returns a copy of a portion of an array.
/** Fills a portion of the given array with the given value. * * <P>If possible (i.e., <code>from</code> is 0) this method uses a * backward loop. In this case, it is significantly faster than the * corresponding method in {@link java.util.Arrays}. * * @param array an array. * @param from the starting index of the portion to fill. * @param to the end index of the portion to fill. * @param value the new value for all elements of the specified portion of the array. */ public static void fill( final boolean[] array, final int from, int to, final boolean value ) { ensureFromTo( array, from, to ); if ( from == 0 ) while( to-- != 0 ) array[ to ] = value; else for( int i = from; i < to; i++ ) array[ i ] = value; } /** Returns true if the two arrays are elementwise equal.
/** * Sorts two arrays according to the natural lexicographical ascending order * using quicksort. * * <p> * The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. * Douglas McIlroy, “Engineering a Sort Function”, <i>Software: * Practice and Experience</i>, 23(11), pages 1249−1265, 1993. * * <p> * This method implements a <em>lexicographical</em> sorting of the arguments. * Pairs of elements in the same position in the two provided arrays will be * considered a single key, and permuted accordingly. In the end, either * {@code x[i] < x[i + 1]} or <code>x[i] * == x[i + 1]</code> and {@code y[i] ≤ y[i + 1]}. * * @param x * the first array to be sorted. * @param y * the second array to be sorted. */ public static void quickSort(final boolean[] x, final boolean[] y) { ensureSameLength(x, y); quickSort(x, y, 0, x.length); } protected static class ForkJoinQuickSort2 extends RecursiveAction {
insertionSort( a, from, to ); return; mergeSort( supp, from, mid, a ); mergeSort( supp, mid, to, a );
selectionSort( x, from, to ); return; if ( len > MEDIUM ) { // Big arrays, pseudomedian of 9 int s = len / 8; l = med3( x, l, l + s, l + 2 * s ); m = med3( x, m - s, m, m + s ); n = med3( x, n - 2 * s, n - s, n ); m = med3( x, l, m, n ); // Mid-size, med of 3 int comparison; while ( b <= c && ( comparison = ( !(x[ b ]) && (v) ? -1 : ( (x[ b ]) == (v) ? 0 : 1 ) ) ) <= 0 ) { if ( comparison == 0 ) swap( x, a++, b ); b++; if ( comparison == 0 ) swap( x, c, d-- ); c--; swap( x, b++, c-- ); vecSwap( x, from, b - s, s ); s = Math.min( d - c, n - d - 1 ); vecSwap( x, b, n - s, s ); if ( ( s = b - a ) > 1 ) quickSort( x, from, from + s ); if ( ( s = d - c ) > 1 ) quickSort( x, n - s, n );
final int len = to - from; if (len < PARALLEL_QUICKSORT_NO_FORK) { quickSort(x, from, to); return; while (b <= c && (comparison = (Boolean.compare((x[b]), (v)))) <= 0) { if (comparison == 0) swap(x, a++, b); b++; swap(x, c, d--); c--; swap(x, b++, c--); swap(x, from, b - s, s); s = Math.min(d - c, to - d - 1); swap(x, b, to - s, s);
/** Stores an array fragment to a given print stream. * * @param array an array whose elements will be written to {@code stream}. * @param offset the index of the first element of {@code array} to be written. * @param length the number of elements of {@code array} to be written. * @param stream a print stream. */ public static void storeBooleans(final boolean array[], final int offset, final int length, final PrintStream stream) { it.unimi.dsi.fastutil.booleans.BooleanArrays.ensureOffsetLength(array, offset, length); for(int i = 0; i < length; i++) stream.println(array[offset + i]); } /** Stores an array to a given print stream.
/** Sorts an array according to the natural ascending order using mergesort. * * <p>This sort is guaranteed to be <i>stable</i>: equal elements will not be reordered as a result * of the sort. An array as large as <code>a</code> will be allocated by this method. * @param a the array to be sorted. */ public static void mergeSort( final boolean a[] ) { mergeSort( a, 0, a.length ); } /** Sorts the specified range of elements according to the order induced by the specified
private static void swap(final boolean[] x, final boolean[] y, int a, int b, final int n) { for (int i = 0; i < n; i++, a++, b++) swap(x, y, a, b); }
/** Creates a new byte-buffer input stream by mapping a given file channel. * * @param fileChannel the file channel that will be mapped. * @param mapMode this must be {@link MapMode#READ_ONLY}. * @return a new byte-buffer input stream over the contents of <code>fileChannel</code>. */ public static ByteBufferInpStream map( final FileChannel fileChannel, final MapMode mapMode ) throws IOException { final long size = fileChannel.size(); final int chunks = (int)( ( size + ( CHUNK_SIZE - 1 ) ) / CHUNK_SIZE ); final ByteBuffer[] byteBuffer = new ByteBuffer[ chunks ]; for( int i = 0; i < chunks; i++ ) byteBuffer[ i ] = fileChannel.map( mapMode, i * CHUNK_SIZE, Math.min( CHUNK_SIZE, size - i * CHUNK_SIZE ) ); byteBuffer[ 0 ].position( 0 ); final boolean[] readyToUse = new boolean[ chunks ]; BooleanArrays.fill( readyToUse, true ); return new ByteBufferInpStream( byteBuffer, size, 0, readyToUse ); }
/** * Ensures that this array list can contain the given number of entries without * resizing. * * @param capacity * the new minimum capacity for this array list. */ public void ensureCapacity(final int capacity) { if (capacity <= a.length || a == BooleanArrays.DEFAULT_EMPTY_ARRAY) return; a = BooleanArrays.ensureCapacity(a, capacity, size); assert size <= a.length; } /**
selectionSort(x, from, to); return; if (len > QUICKSORT_MEDIAN_OF_9) { // Big arrays, pseudomedian of 9 int s = len / 8; l = med3(x, l, l + s, l + 2 * s); m = med3(x, m - s, m, m + s); n = med3(x, n - 2 * s, n - s, n); m = med3(x, l, m, n); // Mid-size, med of 3 final boolean v = x[m]; while (b <= c && (comparison = (Boolean.compare((x[b]), (v)))) <= 0) { if (comparison == 0) swap(x, a++, b); b++; swap(x, c, d--); c--; swap(x, b++, c--); swap(x, from, b - s, s); s = Math.min(d - c, to - d - 1); swap(x, b, to - s, s); quickSort(x, from, from + s); if ((s = d - c) > 1) quickSort(x, to - s, to);
insertionSort(a, from, to); return; mergeSort(supp, from, mid, a); mergeSort(supp, mid, to, a);
final int len = to - from; if (len < PARALLEL_QUICKSORT_NO_FORK) { quickSort(x, from, to, comp); return; while (b <= c && (comparison = comp.compare(x[b], v)) <= 0) { if (comparison == 0) swap(x, a++, b); b++; swap(x, c, d--); c--; swap(x, b++, c--); swap(x, from, b - s, s); s = Math.min(d - c, to - d - 1); swap(x, b, to - s, s);
/** Stores an array fragment to a given data output. * * @param array an array whose elements will be written to {@code dataOutput}. * @param offset the index of the first element of {@code array} to be written. * @param length the number of elements of {@code array} to be written. * @param dataOutput a data output. */ public static void storeBooleans(final boolean array[], final int offset, final int length, final DataOutput dataOutput) throws IOException { it.unimi.dsi.fastutil.booleans.BooleanArrays.ensureOffsetLength(array, offset, length); for(int i = 0; i < length; i++) dataOutput.writeBoolean(array[offset + i]); } /** Stores an array to a given data output.
/** Sorts an array according to the order induced by the specified * comparator using mergesort. * * <p>This sort is guaranteed to be <i>stable</i>: equal elements will not be reordered as a result * of the sort. An array as large as <code>a</code> will be allocated by this method. * @param a the array to be sorted. * @param comp the comparator to determine the sorting order. */ public static void mergeSort( final boolean a[], BooleanComparator comp ) { mergeSort( a, 0, a.length, comp ); } /** Shuffles the specified array fragment using the specified pseudorandom number generator.
private static void vecSwap( final boolean[] x, int a, int b, final int n ) { for( int i = 0; i < n; i++, a++, b++ ) swap( x, a, b ); } private static int med3( final boolean x[], final int a, final int b, final int c, BooleanComparator comp ) {
BooleanArrays.fill( removed, false ); System.arraycopy( externalNode, 0, node, 0, size ); int currLevel, leftLevel;
/** * Ensures that an array can contain the given number of entries. * * <p> * If you cannot foresee whether this array will need again to be enlarged, you * should probably use {@code grow()} instead. * * @param array * an array. * @param length * the new minimum length for this array. * @return {@code array}, if it contains {@code length} entries or more; * otherwise, an array with {@code length} entries whose first * {@code array.length} entries are the same as those of {@code array}. */ public static boolean[] ensureCapacity(final boolean[] array, final int length) { return ensureCapacity(array, length, array.length); } /**