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