Integrated new types, tests still work, not used yet
[Mograsim.git] / era.mi / src / era / mi / logic / Util.java
index 6bca6d0..0567138 100644 (file)
-package era.mi.logic;
-
-import java.util.Arrays;
-
-public final class Util
-{
-    
-    @SuppressWarnings("unchecked")
-    public static <T> T[] concat(T[]... arrays)
-    {
-       if (arrays.length == 0)
-           throw new IllegalArgumentException("Cannot concatenate 0 arrays.");
-
-       int length = 0;
-       for (T[] array : arrays)
-           length += array.length;
-
-       T[] newArray = Arrays.copyOf(arrays[0], length);
-       int appendIndex = arrays[0].length;
-       for (int i = 1; i < arrays.length; i++)
-       {
-           System.arraycopy(arrays[i], 0, newArray, appendIndex, arrays[i].length);
-           appendIndex += arrays[i].length;
-       }
-
-       return newArray;
-    }
-
-//     @SuppressWarnings("unchecked")
-//     public static <T> T[][] split(T[] array, int... lengths)
-//     {
-//             //TODO: implement array split again; This version contains an illegal cast
-//             int totalLength = 0;
-//             for(int length : lengths)
-//                     totalLength += length;
-//             
-//             if(totalLength != array.length)
-//                     throw new IllegalArgumentException(); //TODO: add proper error message
-//             
-//             Object[][] newArray = new Object[lengths.length][];
-//             int splitIndex = 0;
-//             for(int i = 0; i < lengths.length; i++)
-//             {
-//                     System.arraycopy(array, splitIndex, newArray, 0, lengths[i]);
-//                     splitIndex += lengths[i];
-//             }
-//             
-//             return (T[][]) newArray;
-//     }
-
-    public static Bit[] and(Bit[] a, Bit[] b)
-    {
-       return binBitOp(a, b, (bA, bB) -> Bit.and(bA, bB));
-    }
-
-    public static Bit[] or(Bit[] a, Bit[] b)
-    {
-       return binBitOp(a, b, (bA, bB) -> Bit.or(bA, bB));
-    }
-
-    public static Bit[] xor(Bit[] a, Bit[] b)
-    {
-       return binBitOp(a, b, (bA, bB) -> Bit.xor(bA, bB));
-    }
-
-    private static Bit[] binBitOp(Bit[] a, Bit[] b, BitOp op)
-    {
-       if (a.length != b.length)
-           throw new IllegalArgumentException("Bit Arrays were not of equal length.");
-       Bit[] out = new Bit[a.length];
-       for (int i = 0; i < a.length; i++)
-       {
-           out[i] = op.execute(a[i], b[i]);
-       }
-       return out;
-    }
-
-    public static Bit[] not(Bit[] a)
-    {
-       Bit[] out = new Bit[a.length];
-       for (int i = 0; i < a.length; i++)
-       {
-           out[i] = a[i].not();
-       }
-       return out;
-    }
-
-    interface BitOp
-    {
-       Bit execute(Bit a, Bit b);
-    }
-}
+package era.mi.logic;\r
+\r
+import java.util.Arrays;\r
+\r
+import era.mi.logic.types.Bit;\r
+\r
+public final class Util\r
+{\r
+\r
+       @SuppressWarnings("unchecked")\r
+       public static <T> T[] concat(T[]... arrays)\r
+       {\r
+               if (arrays.length == 0)\r
+                       throw new IllegalArgumentException("Cannot concatenate 0 arrays.");\r
+\r
+               int length = 0;\r
+               for (T[] array : arrays)\r
+                       length += array.length;\r
+\r
+               T[] newArray = Arrays.copyOf(arrays[0], length);\r
+               int appendIndex = arrays[0].length;\r
+               for (int i = 1; i < arrays.length; i++)\r
+               {\r
+                       System.arraycopy(arrays[i], 0, newArray, appendIndex, arrays[i].length);\r
+                       appendIndex += arrays[i].length;\r
+               }\r
+\r
+               return newArray;\r
+       }\r
+\r
+//     @SuppressWarnings("unchecked")\r
+//     public static <T> T[][] split(T[] array, int... lengths)\r
+//     {\r
+//             //TODO: implement array split again; This version contains an illegal cast\r
+//             int totalLength = 0;\r
+//             for(int length : lengths)\r
+//                     totalLength += length;\r
+//             \r
+//             if(totalLength != array.length)\r
+//                     throw new IllegalArgumentException(); //TODO: add proper error message\r
+//             \r
+//             Object[][] newArray = new Object[lengths.length][];\r
+//             int splitIndex = 0;\r
+//             for(int i = 0; i < lengths.length; i++)\r
+//             {\r
+//                     System.arraycopy(array, splitIndex, newArray, 0, lengths[i]);\r
+//                     splitIndex += lengths[i];\r
+//             }\r
+//             \r
+//             return (T[][]) newArray;\r
+//     }\r
+\r
+       public static Bit[] and(Bit[] a, Bit[] b)\r
+       {\r
+               return binBitOp(a, b, Bit::and);\r
+       }\r
+\r
+       public static Bit[] or(Bit[] a, Bit[] b)\r
+       {\r
+               return binBitOp(a, b, Bit::or);\r
+       }\r
+\r
+       public static Bit[] xor(Bit[] a, Bit[] b)\r
+       {\r
+               return binBitOp(a, b, Bit::xor);\r
+       }\r
+\r
+       private static Bit[] binBitOp(Bit[] a, Bit[] b, BitOp op)\r
+       {\r
+               if (a.length != b.length)\r
+                       throw new IllegalArgumentException("Bit Arrays were not of equal length.");\r
+               Bit[] out = new Bit[a.length];\r
+               for (int i = 0; i < a.length; i++)\r
+               {\r
+                       out[i] = op.execute(a[i], b[i]);\r
+               }\r
+               return out;\r
+       }\r
+\r
+       public static Bit[] not(Bit[] a)\r
+       {\r
+               Bit[] out = new Bit[a.length];\r
+               for (int i = 0; i < a.length; i++)\r
+               {\r
+                       out[i] = a[i].not();\r
+               }\r
+               return out;\r
+       }\r
+\r
+       /**\r
+        * uses the {@link Bit#combineWith(Bit)} method, does not create a new array, the result is stored in the first array.\r
+        * \r
+        * @author Christian Femers\r
+        */\r
+       public static Bit[] combineInto(Bit[] dest, Bit[] addition)\r
+       {\r
+               if (dest.length != addition.length)\r
+                       throw new IllegalArgumentException("Bit Arrays were not of equal length.");\r
+               for (int i = 0; i < addition.length; i++)\r
+               {\r
+                       dest[i] = dest[i].join(addition[i]);\r
+               }\r
+               return dest;\r
+       }\r
+\r
+       interface BitOp\r
+       {\r
+               Bit execute(Bit a, Bit b);\r
+       }\r
+}\r