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