/** * Ensures that this array list can contain the given number of entries without resizing. * * @param capacity the new minimum capacity for this array list. */ @SuppressWarnings("unchecked") public void ensureCapacity(final int capacity) { if (wrapped) a = ObjectArrays.ensureCapacity(a, capacity, size); else { if (capacity > a.length) { final Object t[] = new Object[capacity]; System.arraycopy(a, 0, t, 0, size); a = (K[]) t; } } if (ASSERTS) assert size <= a.length; }
/** * Grows this array list, ensuring that it can contain the given number of entries without * resizing, and in case enlarging it at least by a factor of two. * * @param capacity the new minimum capacity for this array list. */ @SuppressWarnings("unchecked") private void grow(final int capacity) { if (wrapped) a = ObjectArrays.grow(a, capacity, size); else { if (capacity > a.length) { final int newLength = (int) Math .max(Math.min(2L * a.length, it.unimi.dsi.fastutil.Arrays.MAX_ARRAY_SIZE), capacity); final Object t[] = new Object[newLength]; System.arraycopy(a, 0, t, 0, size); a = (K[]) t; } } if (ASSERTS) assert size <= a.length; }
/** * Copies element of this type-specific list into the given array using optimized system calls. * * @param from the start index (inclusive). * @param a the destination array. * @param offset the offset into the destination array where to store the first element copied. * @param length the number of elements to be copied. */ @Override public void getElements(final int from, final Object[] a, final int offset, final int length) { ObjectArrays.ensureOffsetLength(a, offset, length); System.arraycopy(this.a, from, a, offset, length); }
/** * 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. * * <p> * Note that this implementation does not allocate any object, contrarily to the * implementation used to sort primitive types in {@link java.util.Arrays}, * which switches to mergesort on large inputs. * * @param x * the array to be sorted. * */ public static <K> void quickSort(final K[] x) { quickSort(x, 0, x.length); } protected static class ForkJoinQuickSort<K> extends RecursiveAction {
selectionSort(x, from, to, comp); 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, comp); m = med3(x, m - s, m, m + s, comp); n = med3(x, n - 2 * s, n - s, n, comp); m = med3(x, l, m, n, comp); // Mid-size, med of 3 final K v = x[m]; 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); quickSort(x, from, from + s, comp); if ((s = d - c) > 1) quickSort(x, to - s, to, comp);
/** * Sets the length of the given array. * * @param array * an array. * @param length * the new length for the array. * @return {@code array}, if it contains exactly {@code length} entries; * otherwise, if it contains <em>more</em> than {@code length} entries, * an array with {@code length} entries whose entries are the same as * the first {@code length} entries of {@code array}; otherwise, an * array with {@code length} entries whose first {@code array.length} * entries are the same as those of {@code array}. * */ public static <K> K[] setLength(final K[] array, final int length) { if (length == array.length) return array; if (length < array.length) return trim(array, length); return ensureCapacity(array, length); } /**
/** * Unwraps an iterator, returning an array, with a limit on the number of * elements. * * <p> * This method iterates over the given type-specific iterator and returns an * array containing the elements returned by the iterator. At most {@code max} * elements will be returned. * * @param i * a type-specific iterator. * @param max * the maximum number of elements to be unwrapped. * @return an array containing the elements returned by the iterator (at most * {@code max}). */ @SuppressWarnings("unchecked") public static <K> K[] unwrap(final Iterator<? extends K> i, int max) { if (max < 0) throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative"); K array[] = (K[]) new Object[16]; int j = 0; while (max-- != 0 && i.hasNext()) { if (j == array.length) array = ObjectArrays.grow(array, j + 1); array[j++] = i.next(); } return ObjectArrays.trim(array, j); } /**
final int len = to - from; if (len < PARALLEL_QUICKSORT_NO_FORK) { quickSort(x, from, to); return; while (b <= c && (comparison = (((Comparable<K>) (x[b])).compareTo(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);
/** * 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 <K> void quickSort(final K[] x, final K[] y) { ensureSameLength(x, y); quickSort(x, y, 0, x.length); } protected static class ForkJoinQuickSort2<K> extends RecursiveAction {
/** * Returns a copy of a portion of an array. * * @param array * an array. * @param offset * the first element to copy. * @param length * the number of elements to copy. * @return a new array containing {@code length} elements of {@code array} * starting at {@code offset}. */ public static <K> K[] copy(final K[] array, final int offset, final int length) { ensureOffsetLength(array, offset, length); final K[] a = newArray(array, length); System.arraycopy(array, offset, a, 0, length); return a; } /**
public void size(final int size, final T defaultElement) { if (size > this.a.length) this.ensureCapacity(size); if (size > this.size) { ObjectArrays.fill(this.a, this.size, size, defaultElement); this.size = size; } this.usedElements = size; }
/** * Sorts an array according to the order induced by the specified comparator * 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> * Note that this implementation does not allocate any object, contrarily to the * implementation used to sort primitive types in {@link java.util.Arrays}, * which switches to mergesort on large inputs. * * @param x * the array to be sorted. * @param comp * the comparator to determine the sorting order. * */ public static <K> void quickSort(final K[] x, final Comparator<K> comp) { quickSort(x, 0, x.length, comp); } protected static class ForkJoinQuickSortComp<K> extends RecursiveAction {
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 K v = x[m]; while (b <= c && (comparison = (((Comparable<K>) (x[b])).compareTo(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);
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);
@Override public void size(final int size) { if (size > this.a.length) this.ensureCapacity(size); if (size > this.size) { ObjectArrays.fill(this.a, this.size, size, null); this.size = size; } this.usedElements = size; }
/** * Grows the given array to the maximum between the given length and the current * length increased by 50%, provided that the given length is larger than the * current length. * * <p> * If you want complete control on the array growth, you should probably use * {@code ensureCapacity()} instead. * * @param array * an array. * @param length * the new minimum length for this array. * @return {@code array}, if it can contain {@code length} entries; otherwise, * an array with max({@code length},{@code array.length}/φ) entries * whose first {@code array.length} entries are the same as those of * {@code array}. */ public static <K> K[] grow(final K[] array, final int length) { return grow(array, length, array.length); } /**
/** * Adds elements to this type-specific list using optimized system calls. * * @param index the index at which to add elements. * @param a the array containing the elements. * @param offset the offset of the first element to add. * @param length the number of elements to add. */ @Override public void addElements(final int index, final K a[], final int offset, final int length) { ensureIndex(index); ObjectArrays.ensureOffsetLength(a, offset, length); grow(size + length); System.arraycopy(this.a, index, this.a, index + length, size - index); System.arraycopy(a, offset, this.a, index, length); size += length; }
/** * 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 <K> K[] ensureCapacity(final K[] array, final int length) { return ensureCapacity(array, length, array.length); } /**
/** * Sorts the specified range of elements according to the natural ascending * order using a parallel 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 implementation uses a {@link ForkJoinPool} executor service with * {@link Runtime#availableProcessors()} parallel threads. * * @param x * the array to be sorted. * @param from * the index of the first element (inclusive) to be sorted. * @param to * the index of the last element (exclusive) to be sorted. */ public static <K> void parallelQuickSort(final K[] x, final int from, final int to) { if (to - from < PARALLEL_QUICKSORT_NO_FORK) quickSort(x, from, to); else { final ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors()); pool.invoke(new ForkJoinQuickSort<>(x, from, to)); pool.shutdown(); } } /**
final int len = to - from; if (len < QUICKSORT_NO_REC) { selectionSort(x, y, from, to); return; if (len > QUICKSORT_MEDIAN_OF_9) { // Big arrays, pseudomedian of 9 int s = len / 8; l = med3(x, y, l, l + s, l + 2 * s); m = med3(x, y, m - s, m, m + s); n = med3(x, y, n - 2 * s, n - s, n); m = med3(x, y, l, m, n); // Mid-size, med of 3 final K v = x[m], w = y[m]; : t) <= 0) { if (comparison == 0) swap(x, y, a++, b); b++; : t) >= 0) { if (comparison == 0) swap(x, y, c, d--); c--; swap(x, y, b++, c--); swap(x, y, from, b - s, s); s = Math.min(d - c, to - d - 1); swap(x, y, b, to - s, s);