@Override public void sortAscending() { ByteArrays.mergeSort(data.elements()); }
void ensure(int numBytes) { int newCapacity = length + numBytes; if (newCapacity <= data.length) { return; } newCapacity = (int) Math.max(Math.min((long) data.length + (data.length >> 1), Arrays.MAX_ARRAY_SIZE), newCapacity); data = ByteArrays.forceCapacity(data, newCapacity, length); } }
@Override public void sortDescending() { byte[] elements = values.toByteArray(); ByteArrays.parallelQuickSort(elements, reverseDictionarySortComparator); this.values = new ByteArrayList(elements); }
@Override public void write(final int b) { if (position >= array.length) array = ByteArrays.grow(array, position + 1, length); array[position++] = (byte)b; if (length < position) length = position; }
/** 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 storeBytes(final byte array[], final int offset, final int length, final PrintStream stream) { it.unimi.dsi.fastutil.bytes.ByteArrays.ensureOffsetLength(array, offset, length); for(int i = 0; i < length; i++) stream.println(array[offset + i]); } /** Stores an array to a given print stream.
@Override public void write(final byte[] b, final int off, final int len) throws IOException { ByteArrays.ensureOffsetLength(b, off, len); if (position + len > array.length) array = ByteArrays.grow(array, position + len, position); System.arraycopy(b, off, array, position, len); if (position + len > length) length = position += len; }
/** * 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 byte[] unwrap(final ByteIterator i, int max) { if (max < 0) throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative"); byte array[] = new byte[16]; int j = 0; while (max-- != 0 && i.hasNext()) { if (j == array.length) array = ByteArrays.grow(array, j + 1); array[j++] = i.nextByte(); } return ByteArrays.trim(array, j); } /**
@Override public byte[] next() { int length, common; if (!hasNext()) throw new NoSuchElementException(); if (i % ratio == 0) { pos = p[i / ratio]; length = readInt(array, pos); s = ByteArrays.ensureCapacity(s, length, 0); ByteBigArrays.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 = ByteArrays.ensureCapacity(s, length + common, common); ByteBigArrays.copyFromBig(array, pos + count(length) + count(common), s, common, length); pos += count(length) + count(common) + length; length += common; } else { s = ByteArrays.ensureCapacity(s, length = length(i), 0); extract(i, s, 0, length); } } i++; return ByteArrays.copy(s, 0, length); } @Override
insertionSort(a, from, to); return; mergeSort(supp, from, mid, a); mergeSort(supp, mid, to, a);
/** * 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 byte[] setLength(final byte[] array, final int length) { if (length == array.length) return array; if (length < array.length) return trim(array, length); return ensureCapacity(array, length); } /**
/** * 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 byte[] x, final byte[] 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 == ByteArrays.DEFAULT_EMPTY_ARRAY) return; a = ByteArrays.ensureCapacity(a, capacity, size); assert size <= a.length; } /**
/** * 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 byte[] grow(final byte[] array, final int length) { return grow(array, length, array.length); } /**
/** Loads elements from a given data input, storing them in a given array fragment. * * @param dataInput a data input. * @param array an array which will be filled with data from {@code dataInput}. * @param offset the index of the first element of {@code array} to be filled. * @param length the number of elements of {@code array} to be filled. * @return the number of elements actually read from {@code dataInput} (it might be less than {@code length} if {@code dataInput} ends). */ public static int loadBytes(final DataInput dataInput, final byte[] array, final int offset, final int length) throws IOException { it.unimi.dsi.fastutil.bytes.ByteArrays.ensureOffsetLength(array, offset, length); int i = 0; try { for(i = 0; i < length; i++) array[i + offset] = dataInput.readByte(); } catch(EOFException itsOk) {} return i; } /** Loads elements from a given data input, storing them in a given array.
p[n / ratio] = curSize; array = ByteArrays.grow(array, curSize + count(length) + length, curSize); curSize += writeInt(array, length, curSize); length -= common; array = ByteArrays.grow(array, curSize + count(length) + count(common) + length, curSize); curSize += writeInt(array, length, curSize); this.bb = new BackingByteArray( ByteArrays.trim(array, curSize) );
a = ByteArrays.ensureCapacity(a, length, 0); a = ByteArrays.ensureCapacity(a, length + common, common); length += common; } else { a = ByteArrays.ensureCapacity(a, length = length(i), 0); extract(i, a, 0, length); return ByteArrays.copy(a, 0, length);
insertionSort(a, from, to, comp); return; mergeSort(supp, from, mid, comp, a); mergeSort(supp, mid, to, comp, a);
/** * 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 byte[] ensureCapacity(final byte[] array, final int length) { return ensureCapacity(array, length, array.length); } /**
@Override public void enqueue(byte x) { if (size == heap.length) heap = ByteArrays.grow(heap, size + 1); heap[size++] = x; ByteHeaps.upHeap(heap, size, size - 1, c); } @Override
/** * 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 byte[] copy(final byte[] array, final int offset, final int length) { ensureOffsetLength(array, offset, length); final byte[] a = length == 0 ? EMPTY_ARRAY : new byte[length]; System.arraycopy(array, offset, a, 0, length); return a; } /**