Integrated new types, tests still work, not used yet
[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 import era.mi.logic.types.Bit;\r
6 \r
7 public final class Util\r
8 {\r
9 \r
10         @SuppressWarnings("unchecked")\r
11         public static <T> T[] concat(T[]... arrays)\r
12         {\r
13                 if (arrays.length == 0)\r
14                         throw new IllegalArgumentException("Cannot concatenate 0 arrays.");\r
15 \r
16                 int length = 0;\r
17                 for (T[] array : arrays)\r
18                         length += array.length;\r
19 \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
23                 {\r
24                         System.arraycopy(arrays[i], 0, newArray, appendIndex, arrays[i].length);\r
25                         appendIndex += arrays[i].length;\r
26                 }\r
27 \r
28                 return newArray;\r
29         }\r
30 \r
31 //      @SuppressWarnings("unchecked")\r
32 //      public static <T> T[][] split(T[] array, int... lengths)\r
33 //      {\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
38 //              \r
39 //              if(totalLength != array.length)\r
40 //                      throw new IllegalArgumentException(); //TODO: add proper error message\r
41 //              \r
42 //              Object[][] newArray = new Object[lengths.length][];\r
43 //              int splitIndex = 0;\r
44 //              for(int i = 0; i < lengths.length; i++)\r
45 //              {\r
46 //                      System.arraycopy(array, splitIndex, newArray, 0, lengths[i]);\r
47 //                      splitIndex += lengths[i];\r
48 //              }\r
49 //              \r
50 //              return (T[][]) newArray;\r
51 //      }\r
52 \r
53         public static Bit[] and(Bit[] a, Bit[] b)\r
54         {\r
55                 return binBitOp(a, b, Bit::and);\r
56         }\r
57 \r
58         public static Bit[] or(Bit[] a, Bit[] b)\r
59         {\r
60                 return binBitOp(a, b, Bit::or);\r
61         }\r
62 \r
63         public static Bit[] xor(Bit[] a, Bit[] b)\r
64         {\r
65                 return binBitOp(a, b, Bit::xor);\r
66         }\r
67 \r
68         private static Bit[] binBitOp(Bit[] a, Bit[] b, BitOp op)\r
69         {\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
74                 {\r
75                         out[i] = op.execute(a[i], b[i]);\r
76                 }\r
77                 return out;\r
78         }\r
79 \r
80         public static Bit[] not(Bit[] a)\r
81         {\r
82                 Bit[] out = new Bit[a.length];\r
83                 for (int i = 0; i < a.length; i++)\r
84                 {\r
85                         out[i] = a[i].not();\r
86                 }\r
87                 return out;\r
88         }\r
89 \r
90         /**\r
91          * uses the {@link Bit#combineWith(Bit)} method, does not create a new array, the result is stored in the first array.\r
92          * \r
93          * @author Christian Femers\r
94          */\r
95         public static Bit[] combineInto(Bit[] dest, Bit[] addition)\r
96         {\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
100                 {\r
101                         dest[i] = dest[i].join(addition[i]);\r
102                 }\r
103                 return dest;\r
104         }\r
105 \r
106         interface BitOp\r
107         {\r
108                 Bit execute(Bit a, Bit b);\r
109         }\r
110 }\r