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