|
|
@@ -1,158 +1,160 @@
|
|
|
-/*
|
|
|
- * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
- * @copyright Ranides Atterwim
|
|
|
- * @license WTFPL
|
|
|
- * @url http://ranides.net/projects/assira
|
|
|
- */
|
|
|
-package net.ranides.assira.collection.arrays;
|
|
|
-
|
|
|
-public final class IntArrayAllocator {
|
|
|
-
|
|
|
- /**
|
|
|
- * A static, final, empty array.
|
|
|
- */
|
|
|
- public final static int[] EMPTY_ARRAY = {};
|
|
|
-
|
|
|
- private IntArrayAllocator() {
|
|
|
- // utility class
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 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()</code> instead.
|
|
|
- *
|
|
|
- * @param array an array.
|
|
|
- * @param length the new minimum length for this array.
|
|
|
- * @return <code>array</code>, if it contains <code>length</code> entries or
|
|
|
- * more; 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 int[] ensureCapacity(int[] array, int length) {
|
|
|
- return ensureCapacity(array, length, IntArrayUtils.size(array));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Ensures that an array can contain the given number of entries, preserving
|
|
|
- * just a part of the array.
|
|
|
- *
|
|
|
- * @param array an array.
|
|
|
- * @param length the new minimum length for this array.
|
|
|
- * @param preserve the number of elements of the 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, an array with <code>length</code> entries whose *
|
|
|
- * first <code>preserve</code> entries are the same as those * *
|
|
|
- * of <code>array</code>.
|
|
|
- */
|
|
|
- public static int[] ensureCapacity(int[] array, int length, int preserve) {
|
|
|
- if (length > IntArrayUtils.size(array)) {
|
|
|
- int t[] = new int[length];
|
|
|
- System.arraycopy(array, 0, t, 0, preserve);
|
|
|
- return t;
|
|
|
- }
|
|
|
- return array;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * Ensures that a range given by an offset and a length fits an array.
|
|
|
- *
|
|
|
- * <P>This method may be used whenever an array range check is needed.
|
|
|
- *
|
|
|
- * @param array an array.
|
|
|
- * @param offset a start index.
|
|
|
- * @param length a length (the number of elements in the range).
|
|
|
- * @throws IllegalArgumentException if <code>length</code> is negative.
|
|
|
- * @throws ArrayIndexOutOfBoundsException if <code>offset</code> is negative
|
|
|
- * or <code>offset</code>+<code>length</code> is greater than the array
|
|
|
- * length.
|
|
|
- */
|
|
|
- public static void ensureOffsetLength(int[] array, int offset, int length) {
|
|
|
- ArrayAllocator.ensureOffsetLength(IntArrayUtils.size(array), offset, length);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Ensures that a range given by its first (inclusive) and last (exclusive)
|
|
|
- * elements fits an array.
|
|
|
- *
|
|
|
- * <P>This method may be used whenever an array range check is needed.
|
|
|
- *
|
|
|
- * @param array an array.
|
|
|
- * @param begin a start index (inclusive).
|
|
|
- * @param end an end index (exclusive).
|
|
|
- * @throws IllegalArgumentException if <code>from</code> is greater * *
|
|
|
- * than <code>to</code>.
|
|
|
- * @throws ArrayIndexOutOfBoundsException if <code>from</code> * *
|
|
|
- * or <code>to</code> are greater than the array length or negative.
|
|
|
- */
|
|
|
- public static void ensureFromTo(int[] array, int begin, int end) {
|
|
|
- ArrayAllocator.ensureFromTo(IntArrayUtils.size(array), begin, end);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Grows the given array to the maximum between the given length and the
|
|
|
- * current length multiplied by two, 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()</code> instead.
|
|
|
- *
|
|
|
- * @param array an array.
|
|
|
- * @param length the new minimum length for this array.
|
|
|
- * @return <code>array</code>, if it can contain <code>length</code>
|
|
|
- * entries; otherwise, an array with
|
|
|
- * max(<code>length</code>,<code>array.length</code>/φ) entries whose
|
|
|
- * first <code>array.length</code> entries are the same as those * *
|
|
|
- * of <code>array</code>.
|
|
|
- *
|
|
|
- */
|
|
|
- public static int[] grow(int[] array, int length) {
|
|
|
- return grow(array, length, IntArrayUtils.size(array));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Grows the given array to the maximum between the given length and the
|
|
|
- * current length multiplied by two, provided that the given length is
|
|
|
- * larger than the current length, preserving just a part of the array.
|
|
|
- *
|
|
|
- * <P>If you want complete control on the array growth, you should probably
|
|
|
- * use
|
|
|
- * <code>ensureCapacity()</code> instead.
|
|
|
- *
|
|
|
- * @param array an array.
|
|
|
- * @param length the new minimum length for this array.
|
|
|
- * @param preserve the number of elements of the array that must be
|
|
|
- * preserved in case a new allocation is necessary.
|
|
|
- * @return <code>array</code>, if it can contain <code>length</code>
|
|
|
- * entries; otherwise, an array with
|
|
|
- * max(<code>length</code>,<code>array.length</code>/φ) entries whose
|
|
|
- * first <code>preserve</code> entries are the same as those * *
|
|
|
- * of <code>array</code>.
|
|
|
- *
|
|
|
- */
|
|
|
- public static int[] grow(int[] array, int length, int preserve) {
|
|
|
- int n = IntArrayUtils.size(array);
|
|
|
- if (length > n) {
|
|
|
- int newLength = (int) Math.min(Math.max(2L * n, length), ArrayAllocator.MAX_SIZE);
|
|
|
- int t[] = new int[newLength];
|
|
|
- System.arraycopy(array, 0, t, 0, preserve);
|
|
|
- return t;
|
|
|
- }
|
|
|
- return array;
|
|
|
- }
|
|
|
-
|
|
|
- public static int[] trim(int[] array, int length) {
|
|
|
- if (length >= IntArrayUtils.size(array)) {
|
|
|
- return array;
|
|
|
- }
|
|
|
- int target[] = new int[length];
|
|
|
- System.arraycopy(array, 0, target, 0, length);
|
|
|
- return target;
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+/*
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ * @copyright Ranides Atterwim
|
|
|
+ * @license WTFPL
|
|
|
+ * @url http://ranides.net/projects/assira
|
|
|
+ */
|
|
|
+package net.ranides.assira.collection.arrays;
|
|
|
+
|
|
|
+public final class IntArrayAllocator {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * A static, final, empty array.
|
|
|
+ */
|
|
|
+ public final static int[] EMPTY_ARRAY = {};
|
|
|
+
|
|
|
+ private IntArrayAllocator() {
|
|
|
+ // utility class
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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()</code> instead.
|
|
|
+ *
|
|
|
+ * @param array an array.
|
|
|
+ * @param length the new minimum length for this array.
|
|
|
+ * @return <code>array</code>, if it contains <code>length</code> entries or
|
|
|
+ * more; 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 int[] ensureCapacity(int[] array, int length) {
|
|
|
+ return ensureCapacity(array, length, IntArrayUtils.size(array));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Ensures that an array can contain the given number of entries, preserving
|
|
|
+ * just a part of the array.
|
|
|
+ *
|
|
|
+ * @param array an array.
|
|
|
+ * @param length the new minimum length for this array.
|
|
|
+ * @param preserve the number of elements of the 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, an array with <code>length</code> entries whose *
|
|
|
+ * first <code>preserve</code> entries are the same as those * *
|
|
|
+ * of <code>array</code>.
|
|
|
+ */
|
|
|
+ public static int[] ensureCapacity(int[] array, int length, int preserve) {
|
|
|
+ if (length > IntArrayUtils.size(array)) {
|
|
|
+ int t[] = new int[length];
|
|
|
+ if(array != null && preserve>0) {
|
|
|
+ System.arraycopy(array, 0, t, 0, preserve);
|
|
|
+ }
|
|
|
+ return t;
|
|
|
+ }
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Ensures that a range given by an offset and a length fits an array.
|
|
|
+ *
|
|
|
+ * <P>This method may be used whenever an array range check is needed.
|
|
|
+ *
|
|
|
+ * @param array an array.
|
|
|
+ * @param offset a start index.
|
|
|
+ * @param length a length (the number of elements in the range).
|
|
|
+ * @throws IllegalArgumentException if <code>length</code> is negative.
|
|
|
+ * @throws ArrayIndexOutOfBoundsException if <code>offset</code> is negative
|
|
|
+ * or <code>offset</code>+<code>length</code> is greater than the array
|
|
|
+ * length.
|
|
|
+ */
|
|
|
+ public static void ensureOffsetLength(int[] array, int offset, int length) {
|
|
|
+ ArrayAllocator.ensureOffsetLength(IntArrayUtils.size(array), offset, length);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Ensures that a range given by its first (inclusive) and last (exclusive)
|
|
|
+ * elements fits an array.
|
|
|
+ *
|
|
|
+ * <P>This method may be used whenever an array range check is needed.
|
|
|
+ *
|
|
|
+ * @param array an array.
|
|
|
+ * @param begin a start index (inclusive).
|
|
|
+ * @param end an end index (exclusive).
|
|
|
+ * @throws IllegalArgumentException if <code>from</code> is greater * *
|
|
|
+ * than <code>to</code>.
|
|
|
+ * @throws ArrayIndexOutOfBoundsException if <code>from</code> * *
|
|
|
+ * or <code>to</code> are greater than the array length or negative.
|
|
|
+ */
|
|
|
+ public static void ensureFromTo(int[] array, int begin, int end) {
|
|
|
+ ArrayAllocator.ensureFromTo(IntArrayUtils.size(array), begin, end);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Grows the given array to the maximum between the given length and the
|
|
|
+ * current length multiplied by two, 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()</code> instead.
|
|
|
+ *
|
|
|
+ * @param array an array.
|
|
|
+ * @param length the new minimum length for this array.
|
|
|
+ * @return <code>array</code>, if it can contain <code>length</code>
|
|
|
+ * entries; otherwise, an array with
|
|
|
+ * max(<code>length</code>,<code>array.length</code>/φ) entries whose
|
|
|
+ * first <code>array.length</code> entries are the same as those * *
|
|
|
+ * of <code>array</code>.
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public static int[] grow(int[] array, int length) {
|
|
|
+ return grow(array, length, IntArrayUtils.size(array));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Grows the given array to the maximum between the given length and the
|
|
|
+ * current length multiplied by two, provided that the given length is
|
|
|
+ * larger than the current length, preserving just a part of the array.
|
|
|
+ *
|
|
|
+ * <P>If you want complete control on the array growth, you should probably
|
|
|
+ * use
|
|
|
+ * <code>ensureCapacity()</code> instead.
|
|
|
+ *
|
|
|
+ * @param array an array.
|
|
|
+ * @param length the new minimum length for this array.
|
|
|
+ * @param preserve the number of elements of the array that must be
|
|
|
+ * preserved in case a new allocation is necessary.
|
|
|
+ * @return <code>array</code>, if it can contain <code>length</code>
|
|
|
+ * entries; otherwise, an array with
|
|
|
+ * max(<code>length</code>,<code>array.length</code>/φ) entries whose
|
|
|
+ * first <code>preserve</code> entries are the same as those * *
|
|
|
+ * of <code>array</code>.
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public static int[] grow(int[] array, int length, int preserve) {
|
|
|
+ int n = IntArrayUtils.size(array);
|
|
|
+ if (length > n) {
|
|
|
+ int newLength = (int) Math.min(Math.max(2L * n, length), ArrayAllocator.MAX_SIZE);
|
|
|
+ int t[] = new int[newLength];
|
|
|
+ System.arraycopy(array, 0, t, 0, preserve);
|
|
|
+ return t;
|
|
|
+ }
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int[] trim(int[] array, int length) {
|
|
|
+ if (length >= IntArrayUtils.size(array)) {
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+ int target[] = new int[length];
|
|
|
+ System.arraycopy(array, 0, target, 0, length);
|
|
|
+ return target;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|