X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2FUtil.java;h=6bca6d0ef9c542d2789e66fd36392c83a135cbda;hb=49f569b513c36e8ad421fd5a547bf34bd830652a;hp=6f1b93ff92fb4087bb16c8692eeb7e8052bf714f;hpb=670a82cafe6435b2cdaf02e86701fedd14970c1c;p=Mograsim.git diff --git a/era.mi/src/era/mi/logic/Util.java b/era.mi/src/era/mi/logic/Util.java index 6f1b93ff..6bca6d0e 100644 --- a/era.mi/src/era/mi/logic/Util.java +++ b/era.mi/src/era/mi/logic/Util.java @@ -4,28 +4,28 @@ import java.util.Arrays; public final class Util { - - @SuppressWarnings("unchecked") - public static T[] concat(T[]... arrays) + + @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++) { - 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; + 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) // { @@ -47,46 +47,46 @@ public final class Util // // 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) + + 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++) { - Bit[] out = new Bit[a.length]; - for(int i = 0; i < a.length; i++) - { - out[i] = a[i].not(); - } - return out; + out[i] = op.execute(a[i], b[i]); } - - interface BitOp + return out; + } + + public static Bit[] not(Bit[] a) + { + Bit[] out = new Bit[a.length]; + for (int i = 0; i < a.length; i++) { - Bit execute(Bit a, Bit b); + out[i] = a[i].not(); } + return out; + } + + interface BitOp + { + Bit execute(Bit a, Bit b); + } }