1 package era.mi.logic;
\r
3 import java.util.Arrays;
\r
5 public final class Util {
\r
7 @SuppressWarnings("unchecked")
\r
8 public static <T> T[] concat(T[]... arrays) {
\r
9 if (arrays.length == 0)
\r
10 throw new IllegalArgumentException("Cannot concatenate 0 arrays.");
\r
13 for (T[] array : arrays)
\r
14 length += array.length;
\r
16 T[] newArray = Arrays.copyOf(arrays[0], length);
\r
17 int appendIndex = arrays[0].length;
\r
18 for (int i = 1; i < arrays.length; i++) {
\r
19 System.arraycopy(arrays[i], 0, newArray, appendIndex, arrays[i].length);
\r
20 appendIndex += arrays[i].length;
\r
26 // @SuppressWarnings("unchecked")
\r
27 // public static <T> T[][] split(T[] array, int... lengths)
\r
29 // //TODO: implement array split again; This version contains an illegal cast
\r
30 // int totalLength = 0;
\r
31 // for(int length : lengths)
\r
32 // totalLength += length;
\r
34 // if(totalLength != array.length)
\r
35 // throw new IllegalArgumentException(); //TODO: add proper error message
\r
37 // Object[][] newArray = new Object[lengths.length][];
\r
38 // int splitIndex = 0;
\r
39 // for(int i = 0; i < lengths.length; i++)
\r
41 // System.arraycopy(array, splitIndex, newArray, 0, lengths[i]);
\r
42 // splitIndex += lengths[i];
\r
45 // return (T[][]) newArray;
\r
48 public static Bit[] and(Bit[] a, Bit[] b) {
\r
49 return binBitOp(a, b, (bA, bB) -> Bit.and(bA, bB));
\r
52 public static Bit[] or(Bit[] a, Bit[] b) {
\r
53 return binBitOp(a, b, (bA, bB) -> Bit.or(bA, bB));
\r
56 public static Bit[] xor(Bit[] a, Bit[] b) {
\r
57 return binBitOp(a, b, (bA, bB) -> Bit.xor(bA, bB));
\r
60 private static Bit[] binBitOp(Bit[] a, Bit[] b, BitOp op) {
\r
61 if (a.length != b.length)
\r
62 throw new IllegalArgumentException("Bit Arrays were not of equal length.");
\r
63 Bit[] out = new Bit[a.length];
\r
64 for (int i = 0; i < a.length; i++) {
\r
65 out[i] = op.execute(a[i], b[i]);
\r
70 public static Bit[] not(Bit[] a) {
\r
71 Bit[] out = new Bit[a.length];
\r
72 for (int i = 0; i < a.length; i++) {
\r
73 out[i] = a[i].not();
\r
79 * uses the {@link Bit#combineWith(Bit)} method, does not create a new array, the result is stored in the first array.
\r
81 * @author Christian Femers
\r
83 public static Bit[] combineInto(Bit[] dest, Bit[] addition) {
\r
84 if (dest.length != addition.length)
\r
85 throw new IllegalArgumentException("Bit Arrays were not of equal length.");
\r
86 for (int i = 0; i < addition.length; i++) {
\r
87 dest[i] = dest[i].combineWith(addition[i]);
\r
93 Bit execute(Bit a, Bit b);
\r