package ie.dcu.util; import java.util.*; /** * Utilities for arrays. * * @author Kevin McGuinness */ public class ArrayUtils { /** * Remove the given indices from the given collection. * * @param c * The collection. * @param indices * A list of indices (will be sorted). */ public static void remove(Collection c, int[] indices) { Arrays.sort(indices); Iterator iterator = c.iterator(); for (int i = 0; iterator.hasNext(); i++) { iterator.next(); if (Arrays.binarySearch(indices, i) >= 0) { iterator.remove(); } } return; } public static void arrayCopy(Object src, int srcPos, Object dst, int dstPos, int length) { if (dst.getClass().equals(src.getClass())) { System.arraycopy(src, srcPos, dst, dstPos, length); } else { // Try for arrays of different primitive types if (src instanceof byte[] && dst instanceof short[]) { // Copy from an array of bytes to array of shorts arrayCopy((byte[]) src, srcPos, (short[]) dst, dstPos, length); } else if (src instanceof byte[] && dst instanceof int[]) { // Copy from an array of bytes to array of ints arrayCopy((byte[]) src, srcPos, (int[]) dst, dstPos, length); } else if (src instanceof byte[] && dst instanceof long[]) { // Copy from an array of bytes to array of longs arrayCopy((byte[]) src, srcPos, (long[]) dst, dstPos, length); } else if (src instanceof byte[] && dst instanceof float[]) { // Copy from an array of bytes to array of floats arrayCopy((byte[]) src, srcPos, (float[]) dst, dstPos, length); } else if (src instanceof byte[] && dst instanceof double[]) { // Copy from an array of bytes to array of doubles arrayCopy((byte[]) src, srcPos, (double[]) dst, dstPos, length); } else if (src instanceof short[] && dst instanceof byte[]) { // Copy from an array of shorts to array of bytes arrayCopy((short[]) src, srcPos, (byte[]) dst, dstPos, length); } else if (src instanceof short[] && dst instanceof int[]) { // Copy from an array of shorts to array of ints arrayCopy((short[]) src, srcPos, (int[]) dst, dstPos, length); } else if (src instanceof short[] && dst instanceof long[]) { // Copy from an array of shorts to array of longs arrayCopy((short[]) src, srcPos, (long[]) dst, dstPos, length); } else if (src instanceof short[] && dst instanceof float[]) { // Copy from an array of shorts to array of floats arrayCopy((short[]) src, srcPos, (float[]) dst, dstPos, length); } else if (src instanceof short[] && dst instanceof double[]) { // Copy from an array of shorts to array of doubles arrayCopy((short[]) src, srcPos, (double[]) dst, dstPos, length); } else if (src instanceof int[] && dst instanceof byte[]) { // Copy from an array of ints to array of bytes arrayCopy((int[]) src, srcPos, (byte[]) dst, dstPos, length); } else if (src instanceof int[] && dst instanceof short[]) { // Copy from an array of ints to array of shorts arrayCopy((int[]) src, srcPos, (short[]) dst, dstPos, length); } else if (src instanceof int[] && dst instanceof long[]) { // Copy from an array of ints to array of longs arrayCopy((int[]) src, srcPos, (long[]) dst, dstPos, length); } else if (src instanceof int[] && dst instanceof float[]) { // Copy from an array of ints to array of floats arrayCopy((int[]) src, srcPos, (float[]) dst, dstPos, length); } else if (src instanceof int[] && dst instanceof double[]) { // Copy from an array of ints to array of doubles arrayCopy((int[]) src, srcPos, (double[]) dst, dstPos, length); } else if (src instanceof long[] && dst instanceof byte[]) { // Copy from an array of longs to array of bytes arrayCopy((long[]) src, srcPos, (byte[]) dst, dstPos, length); } else if (src instanceof long[] && dst instanceof short[]) { // Copy from an array of longs to array of shorts arrayCopy((long[]) src, srcPos, (short[]) dst, dstPos, length); } else if (src instanceof long[] && dst instanceof int[]) { // Copy from an array of longs to array of ints arrayCopy((long[]) src, srcPos, (int[]) dst, dstPos, length); } else if (src instanceof long[] && dst instanceof float[]) { // Copy from an array of longs to array of floats arrayCopy((long[]) src, srcPos, (float[]) dst, dstPos, length); } else if (src instanceof long[] && dst instanceof double[]) { // Copy from an array of longs to array of doubles arrayCopy((long[]) src, srcPos, (double[]) dst, dstPos, length); } else if (src instanceof float[] && dst instanceof byte[]) { // Copy from an array of floats to array of bytes arrayCopy((float[]) src, srcPos, (byte[]) dst, dstPos, length); } else if (src instanceof float[] && dst instanceof short[]) { // Copy from an array of floats to array of shorts arrayCopy((float[]) src, srcPos, (short[]) dst, dstPos, length); } else if (src instanceof float[] && dst instanceof int[]) { // Copy from an array of floats to array of ints arrayCopy((float[]) src, srcPos, (int[]) dst, dstPos, length); } else if (src instanceof float[] && dst instanceof long[]) { // Copy from an array of floats to array of longs arrayCopy((float[]) src, srcPos, (long[]) dst, dstPos, length); } else if (src instanceof float[] && dst instanceof double[]) { // Copy from an array of floats to array of doubles arrayCopy((float[]) src, srcPos, (double[]) dst, dstPos, length); } else if (src instanceof double[] && dst instanceof byte[]) { // Copy from an array of doubles to array of bytes arrayCopy((double[]) src, srcPos, (byte[]) dst, dstPos, length); } else if (src instanceof double[] && dst instanceof short[]) { // Copy from an array of doubles to array of shorts arrayCopy((double[]) src, srcPos, (short[]) dst, dstPos, length); } else if (src instanceof double[] && dst instanceof int[]) { // Copy from an array of doubles to array of ints arrayCopy((double[]) src, srcPos, (int[]) dst, dstPos, length); } else if (src instanceof double[] && dst instanceof long[]) { // Copy from an array of doubles to array of longs arrayCopy((double[]) src, srcPos, (long[]) dst, dstPos, length); } else if (src instanceof double[] && dst instanceof float[]) { // Copy from an array of doubles to array of floats arrayCopy((double[]) src, srcPos, (float[]) dst, dstPos, length); } else { throw new ArrayStoreException(); } } } public static void arrayCopy(byte[] src, int srcPos, short[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (short) src[srcPos++]; } } public static void arrayCopy(byte[] src, int srcPos, int[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (int) src[srcPos++]; } } public static void arrayCopy(byte[] src, int srcPos, long[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (long) src[srcPos++]; } } public static void arrayCopy(byte[] src, int srcPos, float[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (float) src[srcPos++]; } } public static void arrayCopy(byte[] src, int srcPos, double[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (double) src[srcPos++]; } } public static void arrayCopy(short[] src, int srcPos, byte[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (byte) src[srcPos++]; } } public static void arrayCopy(short[] src, int srcPos, int[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (int) src[srcPos++]; } } public static void arrayCopy(short[] src, int srcPos, long[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (long) src[srcPos++]; } } public static void arrayCopy(short[] src, int srcPos, float[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (float) src[srcPos++]; } } public static void arrayCopy(short[] src, int srcPos, double[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (double) src[srcPos++]; } } public static void arrayCopy(int[] src, int srcPos, byte[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (byte) src[srcPos++]; } } public static void arrayCopy(int[] src, int srcPos, short[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (short) src[srcPos++]; } } public static void arrayCopy(int[] src, int srcPos, long[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (long) src[srcPos++]; } } public static void arrayCopy(int[] src, int srcPos, float[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (float) src[srcPos++]; } } public static void arrayCopy(int[] src, int srcPos, double[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (double) src[srcPos++]; } } public static void arrayCopy(long[] src, int srcPos, byte[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (byte) src[srcPos++]; } } public static void arrayCopy(long[] src, int srcPos, short[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (short) src[srcPos++]; } } public static void arrayCopy(long[] src, int srcPos, int[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (int) src[srcPos++]; } } public static void arrayCopy(long[] src, int srcPos, float[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (float) src[srcPos++]; } } public static void arrayCopy(long[] src, int srcPos, double[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (double) src[srcPos++]; } } public static void arrayCopy(float[] src, int srcPos, byte[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (byte) src[srcPos++]; } } public static void arrayCopy(float[] src, int srcPos, short[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (short) src[srcPos++]; } } public static void arrayCopy(float[] src, int srcPos, int[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (int) src[srcPos++]; } } public static void arrayCopy(float[] src, int srcPos, long[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (long) src[srcPos++]; } } public static void arrayCopy(float[] src, int srcPos, double[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (double) src[srcPos++]; } } public static void arrayCopy(double[] src, int srcPos, byte[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (byte) src[srcPos++]; } } public static void arrayCopy(double[] src, int srcPos, short[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (short) src[srcPos++]; } } public static void arrayCopy(double[] src, int srcPos, int[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (int) src[srcPos++]; } } public static void arrayCopy(double[] src, int srcPos, long[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (long) src[srcPos++]; } } public static void arrayCopy(double[] src, int srcPos, float[] dst, int dstPos, int length) { checkCopy(src.length, srcPos, dst.length, dstPos, length); for (int i = 0; i < length; i++) { dst[dstPos++] = (float) src[srcPos++]; } } private static void checkCopy(int srcLen, int srcPos, int dstLen, int dstPos, int length) { if (srcPos < 0) { throw new IllegalArgumentException("srcPos < 0"); } if (dstPos < 0) { throw new IllegalArgumentException("dstPos < 0"); } if (length < 0) { throw new IllegalArgumentException("length < 0"); } if (srcPos + length > srcLen) { throw new IllegalArgumentException("srcPos + length > src.length"); } if (dstPos + length > dstLen) { throw new IllegalArgumentException("dstPos + length > dst.length"); } } public static void main(String[] args) { int[] srcArray = { 1, 2, 3, 4, 5 }; short[] dstArray = new short[srcArray.length]; arrayCopy(srcArray, 0, dstArray, 0, srcArray.length); Object srcObj = srcArray; Object dstObj = dstArray; arrayCopy(srcObj, 0, dstObj, 0, srcArray.length); } }