private void initializeRankOfDictionaryIds() { final int dictionarySize = dictionary.size(); rankOfDictionaryIds = IntStream.range(0, dictionarySize).toArray(); IntArrays.quickSort( rankOfDictionaryIds, (i1, i2) -> Comparators.<String>naturalNullsFirst().compare(dictionary.get(i1), dictionary.get(i2)) ); IntArrayUtils.inverse(rankOfDictionaryIds); }
private void assertIndexMatchesVals(ColumnarLongs indexed, long[] vals) { Assert.assertEquals(vals.length, indexed.size()); // sequential access int[] indices = new int[vals.length]; for (int i = 0; i < indexed.size(); ++i) { Assert.assertEquals(vals[i], indexed.get(i)); indices[i] = i; } // random access, limited to 1000 elements for large lists (every element would take too long) IntArrays.shuffle(indices, ThreadLocalRandom.current()); final int limit = Math.min(indexed.size(), 1000); for (int i = 0; i < limit; ++i) { int k = indices[i]; Assert.assertEquals(vals[k], indexed.get(k)); } }
@Override public void sortDescending() { IntArrays.parallelQuickSort(data.elements(), reverseIntComparator); }
@Override public void enqueue(final int x) { if (inv[x] >= 0) throw new IllegalArgumentException("Index " + x + " belongs to the queue"); if (size == heap.length) heap = IntArrays.grow(heap, size + 1); inv[heap[size] = x] = size++; IntIndirectHeaps.upHeap(refArray, heap, inv, size, size - 1, c); } @Override
@Override public int nextInt() { curr++; outdegree = bg.sample(); successors.clear(); if (! loops) successors.add(curr); for(int i = 0; i < outdegree; i++) while(! successors.add(random.nextInt(n))); if (! loops) successors.remove(curr); successorArray = IntArrays.grow(successorArray, outdegree); successors.toArray(successorArray); IntArrays.quickSort(successorArray, 0, outdegree); return curr; }
@Override public int[] successorArray() { if (outdegree < 0) { final int d = it0.outdegree(); final int[] s = it0.successorArray(); successors.clear(); for (int i = 0; i < d; i++) { final LazyIntIterator s1 = g1.successors(s[i]); int x; while ((x = s1.nextInt()) >= 0) successors.add(x); } outdegree = successors.size(); succ = IntArrays.ensureCapacity(succ, outdegree, 0); successors.toArray(succ); IntArrays.quickSort(succ, 0, outdegree); } return succ; }
/** Unwraps the elements returned by a lazy iterator into a new array. * * <p>If you need the resulting array to contain the * elements returned by <code>lazyIntIterator</code>, but some more elements set to zero * would cause no harm, consider using {@link #unwrapLoosely(LazyIntIterator)}, which * usually avoids a final call to {@link IntArrays#trim(int[], int)}. * * @param lazyIntIterator a lazy integer iterator. * @return an array containing the elements returned by <code>lazyIntIterator</code>. * @see #unwrapLoosely(LazyIntIterator) */ public static int[] unwrap(final LazyIntIterator lazyIntIterator) { int array[] = new int[16]; int j = 0, t; while((t = lazyIntIterator.nextInt()) != -1) { if (j == array.length) array = IntArrays.grow(array, j + 1); array[j++] = t; } return IntArrays.trim(array, j); }
/** Trims the backing array so that it has exactly {@link #size()} elements. */ public void trim() { heap = IntArrays.trim(heap, size); } @Override
/** * 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 int[] setLength(final int[] array, final int length) { if (length == array.length) return array; if (length < array.length) return trim(array, length); return ensureCapacity(array, length); } /**
/** * Sorts the specified array using indirect radix sort. * * <p> * The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, * Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, * <i>Computing Systems</i>, 6(1), pages 5−27 (1993). * * <p> * This method implement an <em>indirect</em> sort. The elements of {@code perm} * (which must be exactly the numbers in the interval {@code [0..perm.length)}) * will be permuted so that {@code a[perm[i]] ≤ a[perm[i + 1]]}. * * <p> * This implementation will allocate, in the stable case, a support array as * large as {@code perm} (note that the stable version is slightly faster). * * @param perm * a permutation array indexing {@code a}. * @param a * the array to be sorted. * @param stable * whether the sorting algorithm should be stable. */ public static void radixSortIndirect(final int[] perm, final int[] a, final boolean stable) { radixSortIndirect(perm, a, 0, perm.length, stable); } /**
ensureSameLength(a, b); radixSortIndirect(perm, a, b, 0, a.length, stable);
@Override public void enqueue(int x) { if (size == heap.length) heap = IntArrays.grow(heap, size + 1); heap[size++] = x; IntHeaps.upHeap(heap, size, size - 1, c); } @Override
/** Trims the backing array so that it has exactly {@link #size()} elements. */ public void trim() { heap = IntArrays.trim(heap, size); } @Override
public BottomSketch(String str, int nGramSize, int k, boolean doReverseCompliment) { int[] hashes = HashUtils.computeSequenceHashes(str, nGramSize, doReverseCompliment); k = Math.min(k, hashes.length); int[] perm = new int[hashes.length]; for (int iter=0; iter<hashes.length; iter++) perm[iter] = iter; //sort the array IntArrays.radixSortIndirect(perm, hashes, true); hashPositions = new int[k]; for (int iter=0; iter<k; iter++) { int index = perm[iter]; hashPositions[iter] = hashes[index]; } }
IntArrays.quickSort(sortedPositions, 0, sortedPositions.length, new AbstractIntComparator()
@Override public void enqueue(final int x) { if (inv[x] >= 0) throw new IllegalArgumentException("Index " + x + " belongs to the queue"); if (size == heap.length) heap = IntArrays.grow(heap, size + 1); inv[heap[size] = x] = size++; ObjectIndirectHeaps.upHeap(refArray, heap, inv, size, size - 1, c); } @Override
@Override public void sortAscending() { IntArrays.parallelQuickSort(data.elements()); }
private void assertIndexMatchesVals(ColumnarFloats indexed, float[] vals) { Assert.assertEquals(vals.length, indexed.size()); // sequential access int[] indices = new int[vals.length]; for (int i = 0; i < indexed.size(); ++i) { Assert.assertEquals(vals[i], indexed.get(i), DELTA); indices[i] = i; } // random access, limited to 1000 elements for large lists (every element would take too long) IntArrays.shuffle(indices, ThreadLocalRandom.current()); final int limit = Math.min(indexed.size(), 1000); for (int i = 0; i < limit; ++i) { int k = indices[i]; Assert.assertEquals(vals[k], indexed.get(k), DELTA); } }
/** Trims the backing array so that it has exactly {@link #size()} elements. */ public void trim() { array = IntArrays.trim(array, size); } @Override
IntArrays.radixSortIndirect(perm, hashes, true);