6f1b93ff92fb4087bb16c8692eeb7e8052bf714f
[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         
8         @SuppressWarnings("unchecked")
9         public static <T> T[] concat(T[]... arrays)
10         {
11                 if(arrays.length == 0)
12                         throw new IllegalArgumentException("Cannot concatenate 0 arrays.");
13                 
14                 int length = 0;
15                 for(T[] array : arrays)
16                         length += array.length;
17                 
18                 T[] newArray = Arrays.copyOf(arrays[0], length);
19                 int appendIndex = arrays[0].length;
20                 for(int i = 1; i < arrays.length; i++)
21                 {
22                         System.arraycopy(arrays[i], 0, newArray, appendIndex, arrays[i].length);
23                         appendIndex += arrays[i].length;
24                 }
25                 
26                 return newArray;
27         }
28
29 //      @SuppressWarnings("unchecked")
30 //      public static <T> T[][] split(T[] array, int... lengths)
31 //      {
32 //              //TODO: implement array split again; This version contains an illegal cast
33 //              int totalLength = 0;
34 //              for(int length : lengths)
35 //                      totalLength += length;
36 //              
37 //              if(totalLength != array.length)
38 //                      throw new IllegalArgumentException(); //TODO: add proper error message
39 //              
40 //              Object[][] newArray = new Object[lengths.length][];
41 //              int splitIndex = 0;
42 //              for(int i = 0; i < lengths.length; i++)
43 //              {
44 //                      System.arraycopy(array, splitIndex, newArray, 0, lengths[i]);
45 //                      splitIndex += lengths[i];
46 //              }
47 //              
48 //              return (T[][]) newArray;
49 //      }
50         
51         public static Bit[] and(Bit[] a, Bit[] b)
52         {
53                 return binBitOp(a, b, (bA, bB) -> Bit.and(bA, bB));
54         }
55         
56         public static Bit[] or(Bit[] a, Bit[] b)
57         {
58                 return binBitOp(a, b, (bA, bB) -> Bit.or(bA, bB));
59         }
60         
61         public static Bit[] xor(Bit[] a, Bit[] b)
62         {
63                 return binBitOp(a, b, (bA, bB) -> Bit.xor(bA, bB));
64         }
65         
66         private static Bit[] binBitOp(Bit[] a, Bit[] b, BitOp op)
67         {
68                 if(a.length != b.length)
69                         throw new IllegalArgumentException("Bit Arrays were not of equal length.");
70                 Bit[] out = new Bit[a.length];
71                 for(int i = 0; i < a.length; i++)
72                 {
73                         out[i] = op.execute(a[i], b[i]);
74                 }
75                 return out;
76         }
77         
78         public static Bit[] not(Bit[] a)
79         {
80                 Bit[] out = new Bit[a.length];
81                 for(int i = 0; i < a.length; i++)
82                 {
83                         out[i] = a[i].not();
84                 }
85                 return out;
86         }
87         
88         interface BitOp
89         {
90                 Bit execute(Bit a, Bit b);
91         }
92 }