/** * Ensures that a big array can contain the given number of entries, preserving just a part of the * big array. * * <p><strong>Warning:</strong> the returned array might use part of the segments of the original * array, which must be considered read-only after calling this method. * * @param array a big array. * @param length the new minimum length for this big array. * @param preserve the number of elements of the big array that must be preserved in case a new * allocation is necessary. * @return <code>array</code>, if it can contain <code>length</code> entries or more; otherwise, a * big array with <code>length</code> entries whose first <code>preserve</code> entries are * the same as those of <code>array</code>. */ public static boolean[][] ensureCapacity( final boolean[][] array, final long length, final long preserve) { final long oldLength = length(array); if (length > oldLength) { final int valid = array.length - (array.length == 0 || array.length > 0 && array[array.length - 1].length == SEGMENT_SIZE ? 0 : 1); final int baseLength = (int) ((length + SEGMENT_MASK) / SEGMENT_SIZE); final boolean[][] base = Arrays.copyOf(array, baseLength); final int residual = (int) (length & SEGMENT_MASK); if (residual != 0) { for (int i = valid; i < baseLength - 1; i++) base[i] = new boolean[SEGMENT_SIZE]; base[baseLength - 1] = new boolean[residual]; } else for (int i = valid; i < baseLength; i++) base[i] = new boolean[SEGMENT_SIZE]; if (preserve - (valid * (long) SEGMENT_SIZE) > 0) copy( array, valid * (long) SEGMENT_SIZE, base, valid * (long) SEGMENT_SIZE, preserve - (valid * (long) SEGMENT_SIZE)); return base; } return array; }
/** * Returns a copy of a portion of a big array. * * @param array a big array. * @param offset the first element to copy. * @param length the number of elements to copy. * @return a new big array containing <code>length</code> elements of <code>array</code> starting * at <code>offset</code>. */ public static boolean[][] copy(final boolean[][] array, final long offset, final long length) { ensureOffsetLength(array, offset, length); final boolean[][] a = newBigArray(length); copy(array, offset, a, 0, length); return a; }