88b2393f9b95a092920653d7bdea8ebcfa0270fb
[Mograsim.git] / era.mi / src / era / mi / logic / Util.java
1 package era.mi.logic;\r
2 \r
3 import java.util.Arrays;\r
4 \r
5 public final class Util\r
6 {\r
7 \r
8         @SuppressWarnings("unchecked")\r
9         public static <T> T[] concat(T[]... arrays)\r
10         {\r
11                 if (arrays.length == 0)\r
12                         throw new IllegalArgumentException("Cannot concatenate 0 arrays.");\r
13 \r
14                 int length = 0;\r
15                 for (T[] array : arrays)\r
16                         length += array.length;\r
17 \r
18                 T[] newArray = Arrays.copyOf(arrays[0], length);\r
19                 int appendIndex = arrays[0].length;\r
20                 for (int i = 1; i < arrays.length; i++)\r
21                 {\r
22                         System.arraycopy(arrays[i], 0, newArray, appendIndex, arrays[i].length);\r
23                         appendIndex += arrays[i].length;\r
24                 }\r
25 \r
26                 return newArray;\r
27         }\r
28 \r
29 //      @SuppressWarnings("unchecked")\r
30 //      public static <T> T[][] split(T[] array, int... lengths)\r
31 //      {\r
32 //              //TODO: implement array split again; This version contains an illegal cast\r
33 //              int totalLength = 0;\r
34 //              for(int length : lengths)\r
35 //                      totalLength += length;\r
36 //              \r
37 //              if(totalLength != array.length)\r
38 //                      throw new IllegalArgumentException(); //TODO: add proper error message\r
39 //              \r
40 //              Object[][] newArray = new Object[lengths.length][];\r
41 //              int splitIndex = 0;\r
42 //              for(int i = 0; i < lengths.length; i++)\r
43 //              {\r
44 //                      System.arraycopy(array, splitIndex, newArray, 0, lengths[i]);\r
45 //                      splitIndex += lengths[i];\r
46 //              }\r
47 //              \r
48 //              return (T[][]) newArray;\r
49 //      }\r
50 \r
51         public static Bit[] and(Bit[] a, Bit[] b)\r
52         {\r
53                 return binBitOp(a, b, (bA, bB) -> Bit.and(bA, bB));\r
54         }\r
55 \r
56         public static Bit[] or(Bit[] a, Bit[] b)\r
57         {\r
58                 return binBitOp(a, b, (bA, bB) -> Bit.or(bA, bB));\r
59         }\r
60 \r
61         public static Bit[] xor(Bit[] a, Bit[] b)\r
62         {\r
63                 return binBitOp(a, b, (bA, bB) -> Bit.xor(bA, bB));\r
64         }\r
65 \r
66         private static Bit[] binBitOp(Bit[] a, Bit[] b, BitOp op)\r
67         {\r
68                 if (a.length != b.length)\r
69                         throw new IllegalArgumentException("Bit Arrays were not of equal length.");\r
70                 Bit[] out = new Bit[a.length];\r
71                 for (int i = 0; i < a.length; i++)\r
72                 {\r
73                         out[i] = op.execute(a[i], b[i]);\r
74                 }\r
75                 return out;\r
76         }\r
77 \r
78         public static Bit[] not(Bit[] a)\r
79         {\r
80                 Bit[] out = new Bit[a.length];\r
81                 for (int i = 0; i < a.length; i++)\r
82                 {\r
83                         out[i] = a[i].not();\r
84                 }\r
85                 return out;\r
86         }\r
87 \r
88         /**\r
89          * uses the {@link Bit#combineWith(Bit)} method, does not create a new array, the result is stored in the first array.\r
90          * \r
91          * @author Christian Femers\r
92          */\r
93         public static Bit[] combineInto(Bit[] dest, Bit[] addition)\r
94         {\r
95                 if (dest.length != addition.length)\r
96                         throw new IllegalArgumentException("Bit Arrays were not of equal length.");\r
97                 for (int i = 0; i < addition.length; i++)\r
98                 {\r
99                         dest[i] = dest[i].combineWith(addition[i]);\r
100                 }\r
101                 return dest;\r
102         }\r
103 \r
104         interface BitOp\r
105         {\r
106                 Bit execute(Bit a, Bit b);\r
107         }\r
108 }\r