X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2FUtil.java;fp=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2FUtil.java;h=d621a44c69de41eeb9c3a9eceb8f4a615b2998f0;hb=e7180dd9040734555e6a07f2561fb688118b7002;hp=05671387e79a111d978d5db843d71bbe1dee550d;hpb=70afb26181aba8de30ea6f796ea5c2a573d9ecab;p=Mograsim.git diff --git a/era.mi/src/era/mi/logic/Util.java b/era.mi/src/era/mi/logic/Util.java index 05671387..d621a44c 100644 --- a/era.mi/src/era/mi/logic/Util.java +++ b/era.mi/src/era/mi/logic/Util.java @@ -1,110 +1,110 @@ -package era.mi.logic; - -import java.util.Arrays; - -import era.mi.logic.types.Bit; - -public final class Util -{ - - @SuppressWarnings("unchecked") - public static 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[][] 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, Bit::and); - } - - public static Bit[] or(Bit[] a, Bit[] b) - { - return binBitOp(a, b, Bit::or); - } - - public static Bit[] xor(Bit[] a, Bit[] b) - { - return binBitOp(a, b, Bit::xor); - } - - 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; - } - - /** - * uses the {@link Bit#combineWith(Bit)} method, does not create a new array, the result is stored in the first array. - * - * @author Christian Femers - */ - public static Bit[] combineInto(Bit[] dest, Bit[] addition) - { - if (dest.length != addition.length) - throw new IllegalArgumentException("Bit Arrays were not of equal length."); - for (int i = 0; i < addition.length; i++) - { - dest[i] = dest[i].join(addition[i]); - } - return dest; - } - - interface BitOp - { - Bit execute(Bit a, Bit b); - } -} +package era.mi.logic; + +import java.util.Arrays; + +import era.mi.logic.types.Bit; + +public final class Util +{ + + @SuppressWarnings("unchecked") + public static 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[][] 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, Bit::and); + } + + public static Bit[] or(Bit[] a, Bit[] b) + { + return binBitOp(a, b, Bit::or); + } + + public static Bit[] xor(Bit[] a, Bit[] b) + { + return binBitOp(a, b, Bit::xor); + } + + 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; + } + + /** + * uses the {@link Bit#combineWith(Bit)} method, does not create a new array, the result is stored in the first array. + * + * @author Christian Femers + */ + public static Bit[] combineInto(Bit[] dest, Bit[] addition) + { + if (dest.length != addition.length) + throw new IllegalArgumentException("Bit Arrays were not of equal length."); + for (int i = 0; i < addition.length; i++) + { + dest[i] = dest[i].join(addition[i]); + } + return dest; + } + + interface BitOp + { + Bit execute(Bit a, Bit b); + } +}