/** * Trims the given big array to the given length. * * <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 maximum length for the big array. * @return <code>array</code>, if it contains <code>length</code> entries or less; otherwise, a * big array with <code>length</code> entries whose entries are the same as the first <code> * length</code> entries of <code>array</code>. */ public static boolean[][] trim(final boolean[][] array, final long length) { final long oldLength = length(array); if (length >= oldLength) return array; 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) base[baseLength - 1] = BooleanArrays.trim(base[baseLength - 1], residual); return base; }