Last active
June 10, 2022 19:40
-
-
Save lesleh/7725718 to your computer and use it in GitHub Desktop.
Java generics are so horribly broken.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ONE implementation for everything | |
public static T[][] chunkArray<T>(T[] array, int chunkSize) { | |
int numOfChunks = (int)Math.Ceiling((double)array.Length / chunkSize); | |
T[][] output = new T[numOfChunks][]; | |
for(int i = 0; i < numOfChunks; ++i) { | |
int start = i * chunkSize; | |
int length = Math.Min(array.Length - start, chunkSize); | |
T[] temp = new T[length]; | |
Array.Copy(array, start, temp, 0, length); | |
output[i] = temp; | |
} | |
return output; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int[][] chunkArray(int[] array, int chunkSize) { | |
int numOfChunks = (int)Math.ceil((double)array.length / chunkSize); | |
int[][] output = new int[numOfChunks][]; | |
for(int i = 0; i < numOfChunks; ++i) { | |
int start = i * chunkSize; | |
int length = Math.min(array.length - start, chunkSize); | |
int[] temp = new int[length]; | |
System.arraycopy(array, start, temp, 0, length); | |
output[i] = temp; | |
} | |
return output; | |
} | |
public static long[][] chunkArray(long[] array, int chunkSize) { | |
int numOfChunks = (int)Math.ceil((double)array.length / chunkSize); | |
long[][] output = new long[numOfChunks][]; | |
for(int i = 0; i < numOfChunks; ++i) { | |
int start = i * chunkSize; | |
int length = Math.min(array.length - start, chunkSize); | |
long[] temp = new long[length]; | |
System.arraycopy(array, start, temp, 0, length); | |
output[i] = temp; | |
} | |
return output; | |
} | |
public static char[][] chunkArray(char[] array, int chunkSize) { | |
int numOfChunks = (int)Math.ceil((double)array.length / chunkSize); | |
char[][] output = new char[numOfChunks][]; | |
for(int i = 0; i < numOfChunks; ++i) { | |
int start = i * chunkSize; | |
int length = Math.min(array.length - start, chunkSize); | |
char[] temp = new char[length]; | |
System.arraycopy(array, start, temp, 0, length); | |
output[i] = temp; | |
} | |
return output; | |
} | |
public static byte[][] chunkArray(byte[] array, int chunkSize) { | |
int numOfChunks = (int)Math.ceil((double)array.length / chunkSize); | |
byte[][] output = new byte[numOfChunks][]; | |
for(int i = 0; i < numOfChunks; ++i) { | |
int start = i * chunkSize; | |
int length = Math.min(array.length - start, chunkSize); | |
byte[] temp = new byte[length]; | |
System.arraycopy(array, start, temp, 0, length); | |
output[i] = temp; | |
} | |
return output; | |
} | |
public static boolean[][] chunkArray(boolean[] array, int chunkSize) { | |
int numOfChunks = (int)Math.ceil((double)array.length / chunkSize); | |
boolean[][] output = new boolean[numOfChunks][]; | |
for(int i = 0; i < numOfChunks; ++i) { | |
int start = i * chunkSize; | |
int length = Math.min(array.length - start, chunkSize); | |
boolean[] temp = new boolean[length]; | |
System.arraycopy(array, start, temp, 0, length); | |
output[i] = temp; | |
} | |
return output; | |
} | |
public static double[][] chunkArray(double[] array, int chunkSize) { | |
int numOfChunks = (int)Math.ceil((double)array.length / chunkSize); | |
double[][] output = new double[numOfChunks][]; | |
for(int i = 0; i < numOfChunks; ++i) { | |
int start = i * chunkSize; | |
int length = Math.min(array.length - start, chunkSize); | |
double[] temp = new double[length]; | |
System.arraycopy(array, start, temp, 0, length); | |
output[i] = temp; | |
} | |
return output; | |
} | |
public static float[][] chunkArray(float[] array, int chunkSize) { | |
int numOfChunks = (int)Math.ceil((double)array.length / chunkSize); | |
float[][] output = new float[numOfChunks][]; | |
for(int i = 0; i < numOfChunks; ++i) { | |
int start = i * chunkSize; | |
int length = Math.min(array.length - start, chunkSize); | |
float[] temp = new float[length]; | |
System.arraycopy(array, start, temp, 0, length); | |
output[i] = temp; | |
} | |
return output; | |
} | |
public static short[][] chunkArray(short[] array, int chunkSize) { | |
int numOfChunks = (int)Math.ceil((double)array.length / chunkSize); | |
short[][] output = new short[numOfChunks][]; | |
for(int i = 0; i < numOfChunks; ++i) { | |
int start = i * chunkSize; | |
int length = Math.min(array.length - start, chunkSize); | |
short[] temp = new short[length]; | |
System.arraycopy(array, start, temp, 0, length); | |
output[i] = temp; | |
} | |
return output; | |
} | |
public static <T> T[][] chunkArray(T[] array, int chunkSize) { | |
int numOfChunks = (int)Math.ceil((double)array.length / chunkSize); | |
@SuppressWarnings("unchecked") | |
T[][] output = (T[][])new Object[numOfChunks][]; | |
for(int i = 0; i < numOfChunks; ++i) { | |
int start = i * chunkSize; | |
int length = Math.min(array.length - start, chunkSize); | |
@SuppressWarnings("unchecked") | |
T[] temp = (T[])new Object[length]; | |
System.arraycopy(array, start, temp, 0, length); | |
output[i] = temp; | |
} | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
agreed. I'm about over java.