3 import java.util.Arrays;
5 import era.mi.logic.types.Bit;
7 public final class Util
10 @SuppressWarnings("unchecked")
11 public static <T> T[] concat(T[]... arrays)
13 if (arrays.length == 0)
14 throw new IllegalArgumentException("Cannot concatenate 0 arrays.");
17 for (T[] array : arrays)
18 length += array.length;
20 T[] newArray = Arrays.copyOf(arrays[0], length);
21 int appendIndex = arrays[0].length;
22 for (int i = 1; i < arrays.length; i++)
24 System.arraycopy(arrays[i], 0, newArray, appendIndex, arrays[i].length);
25 appendIndex += arrays[i].length;
31 // @SuppressWarnings("unchecked")
32 // public static <T> T[][] split(T[] array, int... lengths)
34 // //TODO: implement array split again; This version contains an illegal cast
35 // int totalLength = 0;
36 // for(int length : lengths)
37 // totalLength += length;
39 // if(totalLength != array.length)
40 // throw new IllegalArgumentException(); //TODO: add proper error message
42 // Object[][] newArray = new Object[lengths.length][];
43 // int splitIndex = 0;
44 // for(int i = 0; i < lengths.length; i++)
46 // System.arraycopy(array, splitIndex, newArray, 0, lengths[i]);
47 // splitIndex += lengths[i];
50 // return (T[][]) newArray;
53 public static Bit[] and(Bit[] a, Bit[] b)
55 return binBitOp(a, b, Bit::and);
58 public static Bit[] or(Bit[] a, Bit[] b)
60 return binBitOp(a, b, Bit::or);
63 public static Bit[] xor(Bit[] a, Bit[] b)
65 return binBitOp(a, b, Bit::xor);
68 private static Bit[] binBitOp(Bit[] a, Bit[] b, BitOp op)
70 if (a.length != b.length)
71 throw new IllegalArgumentException("Bit Arrays were not of equal length.");
72 Bit[] out = new Bit[a.length];
73 for (int i = 0; i < a.length; i++)
75 out[i] = op.execute(a[i], b[i]);
80 public static Bit[] not(Bit[] a)
82 Bit[] out = new Bit[a.length];
83 for (int i = 0; i < a.length; i++)
91 * uses the {@link Bit#combineWith(Bit)} method, does not create a new array, the result is stored in the first array.
93 * @author Christian Femers
95 public static Bit[] combineInto(Bit[] dest, Bit[] addition)
97 if (dest.length != addition.length)
98 throw new IllegalArgumentException("Bit Arrays were not of equal length.");
99 for (int i = 0; i < addition.length; i++)
101 dest[i] = dest[i].join(addition[i]);
108 Bit execute(Bit a, Bit b);