/** Sets the length of the given array. * * @param array an array. * @param length the new length for the array. * @return <code>array</code>, if it contains exactly <code>length</code> * entries; otherwise, if it contains <em>more</em> than * <code>length</code> entries, an array with <code>length</code> entries * whose entries are the same as the first <code>length</code> entries of * <code>array</code>; otherwise, an array with <code>length</code> entries * whose first <code>array.length</code> entries are the same as those of * <code>array</code>. * */ public static boolean[] setLength( final boolean[] array, final int length ) { if ( length == array.length ) return array; if ( length < array.length ) return trim( array, length ); return ensureCapacity( array, length ); } /** Returns a copy of a portion of an array.
/** * 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 boolean[] setLength(final boolean[] array, final int length) { if (length == array.length) return array; if (length < array.length) return trim(array, length); return ensureCapacity(array, length); } /**
/** * 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}, if it contains {@code length} entries or less; * otherwise, a big array with {@code length} entries whose entries are * the same as the first {@code length} entries of {@code array}. * */ public static boolean[][] trim(final boolean[][] array, final long length) { ensureLength(length); final long oldLength = length(array); if (length >= oldLength) return array; final int baseLength = (int) ((length + SEGMENT_MASK) >>> SEGMENT_SHIFT); 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; } /**
/** * 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 boolean[] unwrap(final BooleanIterator i, int max) { if (max < 0) throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative"); boolean array[] = new boolean[16]; int j = 0; while (max-- != 0 && i.hasNext()) { if (j == array.length) array = BooleanArrays.grow(array, j + 1); array[j++] = i.nextBoolean(); } return BooleanArrays.trim(array, j); } /**