@Override public void sortDescending() { LongArrays.parallelQuickSort(data.elements(), descendingComparator); }
@Override public LongList finishList() { if (entries == null) { return LongLists.EMPTY_LIST; } long[] longs = entries.keySet().toLongArray(); LongArrays.quickSort(longs, new AbstractLongComparator() { @Override public int compare(long k1, long k2) { return Doubles.compare(entries.get(k2), entries.get(k1)); } }); entries = null; return new LongArrayList(longs); } }
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 long v = x[m]; while (b <= c && (comparison = (Long.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);
final int len = to - from; if (len < PARALLEL_QUICKSORT_NO_FORK) { quickSort(x, from, to); return; while (b <= c && (comparison = (Long.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);
/** * 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 long[] x, final long[] y) { ensureSameLength(x, y); quickSort(x, y, 0, x.length); } protected static class ForkJoinQuickSort2 extends RecursiveAction {
/** * 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 long[] grow(final long[] array, final int length) { return grow(array, length, 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}). */ public static long[] unwrap(final LongIterator i, int max) { if (max < 0) throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative"); long array[] = new long[16]; int j = 0; while (max-- != 0 && i.hasNext()) { if (j == array.length) array = LongArrays.grow(array, j + 1); array[j++] = i.nextLong(); } return LongArrays.trim(array, j); } /**
/** * Sorts two arrays according to the natural lexicographical 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 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]}. * * <p> * This implementation uses a {@link ForkJoinPool} executor service with * {@link Runtime#availableProcessors()} parallel threads. * * @param x * the first array to be sorted. * @param y * the second array to be sorted. */ public static void parallelQuickSort(final long[] x, final long[] y) { ensureSameLength(x, y); parallelQuickSort(x, y, 0, x.length); } /**
/** * 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 == LongArrays.DEFAULT_EMPTY_ARRAY) return; a = LongArrays.ensureCapacity(a, capacity, size); assert size <= a.length; } /**
LongArrays.parallelQuickSort(edge); nodeIterator = g.nodeIterator(); final int from0 = x; //Math.min(x, succ[i]); final int to0 = succ[i]; //Math.max(x, succ[i]); final int edge0 = LongArrays.binarySearch(edge, 0, edgesSoFar, ((long)from0 << 32) | to0); if (ASSERTS) assert edge0 >= 0; final int dNext = g.outdegree(to0); final int from1 = to0; //Math.min(x, succ[j]); final int to1 = succNext[j]; //Math.max(x, succ[j]); final int edge1 = LongArrays.binarySearch(edge, 0, edgesSoFar, ((long)from1 << 32) | to1); if (ASSERTS) assert edge1 >= 0; if (currBatch == batchSize) {
/** * 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 long[] setLength(final long[] array, final int length) { if (length == array.length) return array; if (length < array.length) return trim(array, length); return ensureCapacity(array, length); } /**
@Override public long[] next() { int length, common; if (!hasNext()) throw new NoSuchElementException(); if (i % ratio == 0) { pos = p[i / ratio]; length = readInt(array, pos); s = LongArrays.ensureCapacity(s, length, 0); LongBigArrays.copyFromBig(array, pos + count(length), s, 0, length); pos += length + count(length); inSync = true; } else { if (inSync) { length = readInt(array, pos); common = readInt(array, pos + count(length)); s = LongArrays.ensureCapacity(s, length + common, common); LongBigArrays.copyFromBig(array, pos + count(length) + count(common), s, common, length); pos += count(length) + count(common) + length; length += common; } else { s = LongArrays.ensureCapacity(s, length = length(i), 0); extract(i, s, 0, length); } } i++; return LongArrays.copy(s, 0, length); } @Override
/** Reduces as must as possible the size of the backing array. * * @return true if some trimming was actually necessary. */ public boolean trim() { if ( bits.length == numWords( length ) ) return false; bits = LongArrays.setLength( bits, numWords( length ) ); return true; }
/** * Searches an array for the specified value using the binary search algorithm. * The range must be sorted prior to making this call. If it is not sorted, the * results are undefined. If the range contains multiple elements with the * specified value, there is no guarantee which one will be found. * * @param a * the array to be searched. * @param key * the value to be searched for. * @return index of the search key, if it is contained in the array; otherwise, * {@code (-(<i>insertion point</i>) - 1)}. The <i>insertion point</i> * is defined as the the point at which the value would be inserted into * the array: the index of the first element greater than the key, or * the length of the array, if all elements in the array are less than * the specified key. Note that this guarantees that the return value * will be ≥ 0 if and only if the key is found. * @see java.util.Arrays */ public static int binarySearch(final long[] a, final long key) { return binarySearch(a, 0, a.length, key); } /**
@Nonnull @Override public MetricResult measureUser(TestUser user, ResultMap predictions, Mean context) { if (predictions == null || predictions.isEmpty()) { return MetricResult.empty(); } Long2DoubleMap ratings = user.getTestRatings(); long[] ideal = ratings.keySet().toLongArray(); LongArrays.quickSort(ideal, LongComparators.oppositeComparator(LongUtils.keyValueComparator(ratings))); long[] actual = LongUtils.asLongSet(predictions.keySet()).toLongArray(); LongArrays.quickSort(actual, LongComparators.oppositeComparator( LongUtils.keyValueComparator( LongUtils.asLong2DoubleMap(predictions.scoreMap())))); double idealGain = computeDCG(ideal, ratings); double gain = computeDCG(actual, ratings); logger.debug("user {} has gain of {} (ideal {})", user.getUserId(), gain, idealGain); double score = gain / idealGain; synchronized (context) { context.increment(score); } ImmutableMap.Builder<String,Double> results = ImmutableMap.builder(); return MetricResult.fromMap(results.put(columnName, score) .put(columnName + ".Raw", gain) .build()); }
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 long v = x[m], w = y[m]; && (comparison = (t = (Long.compare((x[b]), (v)))) == 0 ? (Long.compare((y[b]), (w))) : t) <= 0) { if (comparison == 0) swap(x, y, a++, b); b++; && (comparison = (t = (Long.compare((x[c]), (v)))) == 0 ? (Long.compare((y[c]), (w))) : 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);
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 enqueue(long x) { if (size == heap.length) heap = LongArrays.grow(heap, size + 1); heap[size++] = x; LongHeaps.upHeap(heap, size, size - 1, c); } @Override
length = a[b].length; if (n % ratio == 0) { p = LongArrays.grow(p, n / ratio + 1); p[n / ratio] = curSize; array = ShortBigArrays.grow(array, curSize + count(length) + length, curSize); this.ratio = ratio; this.array = ShortBigArrays.trim(array, curSize); this.p = LongArrays.trim(p, (n + ratio - 1) / ratio);
/** * 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 long[] ensureCapacity(final long[] array, final int length) { return ensureCapacity(array, length, array.length); } /**