aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/function
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/function')
-rw-r--r--src/jcgp/backend/function/BitwiseLogic.java124
-rw-r--r--src/jcgp/backend/function/BooleanLogic.java199
-rw-r--r--src/jcgp/backend/function/UnsignedInteger.java66
3 files changed, 128 insertions, 261 deletions
diff --git a/src/jcgp/backend/function/BitwiseLogic.java b/src/jcgp/backend/function/BitwiseLogic.java
index 14f4488..a4c2737 100644
--- a/src/jcgp/backend/function/BitwiseLogic.java
+++ b/src/jcgp/backend/function/BitwiseLogic.java
@@ -31,8 +31,8 @@ public class BitwiseLogic extends FunctionSet {
public static class ConstantZero extends Function {
@Override
- public Long run(Connection... connections) {
- return new Long(0);
+ public UnsignedInteger run(Connection... connections) {
+ return new UnsignedInteger(0);
}
@Override
@@ -48,8 +48,8 @@ public class BitwiseLogic extends FunctionSet {
public static class ConstantOne extends Function {
@Override
- public Long run(Connection... connections) {
- return new Long(Long.MAX_VALUE);
+ public UnsignedInteger run(Connection... connections) {
+ return new UnsignedInteger(0xFFFF);
}
@Override
@@ -65,11 +65,11 @@ public class BitwiseLogic extends FunctionSet {
public static class WireA extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- return ((Long) connections[0].getValue()).longValue();
+ return ((UnsignedInteger) connections[0].getValue());
}
}
@@ -86,11 +86,11 @@ public class BitwiseLogic extends FunctionSet {
public static class WireB extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- return ((Long) connections[1].getValue()).longValue();
+ return ((UnsignedInteger) connections[1].getValue());
}
}
@@ -107,11 +107,11 @@ public class BitwiseLogic extends FunctionSet {
public static class NotA extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- return ~((Long) connections[0].getValue()).longValue();
+ return new UnsignedInteger(~((UnsignedInteger) connections[0].getValue()).get());
}
}
@@ -128,11 +128,11 @@ public class BitwiseLogic extends FunctionSet {
public static class NotB extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- return ~((Long) connections[1].getValue()).longValue();
+ return new UnsignedInteger(~((UnsignedInteger) connections[1].getValue()).get());
}
}
@@ -149,15 +149,15 @@ public class BitwiseLogic extends FunctionSet {
public static class And extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- Long arg1 = ((Long) connections[0].getValue());
- Long arg2 = ((Long) connections[1].getValue());
- Long result = arg1 & arg2;
+ UnsignedInteger arg1 = ((UnsignedInteger) connections[0].getValue());
+ UnsignedInteger arg2 = ((UnsignedInteger) connections[1].getValue());
+ Integer result = arg1.get() & arg2.get();
- return result;
+ return new UnsignedInteger(result);
}
}
@@ -174,15 +174,15 @@ public class BitwiseLogic extends FunctionSet {
public static class AndNotA extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- Long arg1 = ((Long) connections[0].getValue());
- Long arg2 = ((Long) connections[1].getValue());
- Long result = ~arg1 & arg2;
+ UnsignedInteger arg1 = ((UnsignedInteger) connections[0].getValue());
+ UnsignedInteger arg2 = ((UnsignedInteger) connections[1].getValue());
+ Integer result = ~(arg1.get()) & arg2.get();
- return result;
+ return new UnsignedInteger(result);
}
}
@@ -199,15 +199,15 @@ public class BitwiseLogic extends FunctionSet {
public static class AndNotB extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- Long arg1 = ((Long) connections[0].getValue());
- Long arg2 = ((Long) connections[1].getValue());
- Long result = arg1 & ~arg2;
+ UnsignedInteger arg1 = ((UnsignedInteger) connections[0].getValue());
+ UnsignedInteger arg2 = ((UnsignedInteger) connections[1].getValue());
+ Integer result = arg1.get() & ~(arg2.get());
- return result;
+ return new UnsignedInteger(result);
}
}
@@ -224,15 +224,15 @@ public class BitwiseLogic extends FunctionSet {
public static class Nor extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- Long arg1 = ((Long) connections[0].getValue());
- Long arg2 = ((Long) connections[1].getValue());
- Long result = arg1 | arg2;
+ UnsignedInteger arg1 = ((UnsignedInteger) connections[0].getValue());
+ UnsignedInteger arg2 = ((UnsignedInteger) connections[1].getValue());
+ Integer result = arg1.get() | arg2.get();
- return ~result;
+ return new UnsignedInteger(~result);
}
}
@@ -249,15 +249,15 @@ public class BitwiseLogic extends FunctionSet {
public static class Xor extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- Long arg1 = ((Long) connections[0].getValue());
- Long arg2 = ((Long) connections[1].getValue());
- Long result = arg1 ^ arg2;
+ UnsignedInteger arg1 = ((UnsignedInteger) connections[0].getValue());
+ UnsignedInteger arg2 = ((UnsignedInteger) connections[1].getValue());
+ Integer result = arg1.get() ^ arg2.get();
- return result;
+ return new UnsignedInteger(result);
}
}
@@ -274,15 +274,15 @@ public class BitwiseLogic extends FunctionSet {
public static class Xnor extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- Long arg1 = ((Long) connections[0].getValue());
- Long arg2 = ((Long) connections[1].getValue());
- Long result = arg1 ^ arg2;
+ UnsignedInteger arg1 = ((UnsignedInteger) connections[0].getValue());
+ UnsignedInteger arg2 = ((UnsignedInteger) connections[1].getValue());
+ Integer result = arg1.get() ^ arg2.get();
- return ~result;
+ return new UnsignedInteger(~result);
}
}
@@ -299,15 +299,15 @@ public class BitwiseLogic extends FunctionSet {
public static class Or extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- Long arg1 = ((Long) connections[0].getValue());
- Long arg2 = ((Long) connections[1].getValue());
- Long result = arg1 | arg2;
+ UnsignedInteger arg1 = ((UnsignedInteger) connections[0].getValue());
+ UnsignedInteger arg2 = ((UnsignedInteger) connections[1].getValue());
+ Integer result = arg1.get() | arg2.get();
- return result;
+ return new UnsignedInteger(result);
}
}
@@ -324,15 +324,15 @@ public class BitwiseLogic extends FunctionSet {
public static class OrNotA extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- Long arg1 = ((Long) connections[0].getValue());
- Long arg2 = ((Long) connections[1].getValue());
- Long result = ~arg1 | arg2;
+ UnsignedInteger arg1 = ((UnsignedInteger) connections[0].getValue());
+ UnsignedInteger arg2 = ((UnsignedInteger) connections[1].getValue());
+ Integer result = ~arg1.get() | arg2.get();
- return result;
+ return new UnsignedInteger(result);
}
}
@@ -349,15 +349,15 @@ public class BitwiseLogic extends FunctionSet {
public static class OrNotB extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- Long arg1 = ((Long) connections[0].getValue());
- Long arg2 = ((Long) connections[1].getValue());
- Long result = arg1 | ~arg2;
+ UnsignedInteger arg1 = ((UnsignedInteger) connections[0].getValue());
+ UnsignedInteger arg2 = ((UnsignedInteger) connections[1].getValue());
+ Integer result = arg1.get() | ~arg2.get();
- return result;
+ return new UnsignedInteger(result);
}
}
@@ -374,15 +374,15 @@ public class BitwiseLogic extends FunctionSet {
public static class Nand extends Function {
@Override
- public Long run(Connection... connections) {
+ public UnsignedInteger run(Connection... connections) {
if (connections.length < getArity()) {
throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
} else {
- Long arg1 = ((Long) connections[0].getValue());
- Long arg2 = ((Long) connections[1].getValue());
- Long result = arg1 & arg2;
+ UnsignedInteger arg1 = ((UnsignedInteger) connections[0].getValue());
+ UnsignedInteger arg2 = ((UnsignedInteger) connections[1].getValue());
+ Integer result = arg1.get() & arg2.get();
- return ~result;
+ return new UnsignedInteger(~result);
}
}
diff --git a/src/jcgp/backend/function/BooleanLogic.java b/src/jcgp/backend/function/BooleanLogic.java
deleted file mode 100644
index 9e7d1ff..0000000
--- a/src/jcgp/backend/function/BooleanLogic.java
+++ /dev/null
@@ -1,199 +0,0 @@
-package jcgp.backend.function;
-
-
-import jcgp.backend.exceptions.InvalidArgumentsException;
-import jcgp.backend.population.Connection;
-
-public class BooleanLogic extends FunctionSet {
-
- public BooleanLogic() {
- name = "1-bit Logic";
- functionList = new Function[]{
- new And(),
- new Or(),
- new Not(),
- new Xor(),
- new Nand(),
- new Nor(),
- new Xnor()};
-
- enableAll();
- }
-
- public static class And extends Function {
- @Override
- public Boolean run(Connection... connections) {
- if (connections.length < 2) {
- throw new InvalidArgumentsException("Not enough connections were given.");
- } else {
- Boolean arg1 = ((Boolean) connections[0].getValue());
- Boolean arg2 = ((Boolean) connections[1].getValue());
- Boolean result = arg1 && arg2;
-
- return result;
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "AND";
- }
- }
-
- public static class Or extends Function {
- @Override
- public Boolean run(Connection... connections) {
- if (connections.length < 2) {
- throw new InvalidArgumentsException("Not enough connections were given.");
- } else {
- Boolean arg1 = ((Boolean) connections[0].getValue());
- Boolean arg2 = ((Boolean) connections[1].getValue());
- Boolean result = arg1 || arg2;
-
- return result;
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "OR";
- }
- }
-
- public static class Not extends Function {
- @Override
- public Boolean run(Connection... connections) {
- if (connections.length < 1) {
- throw new InvalidArgumentsException("Not enough connections were given.");
- } else {
- Boolean arg1 = ((Boolean) connections[0].getValue());
- Boolean result = !arg1;
-
- return result;
- }
- }
-
- @Override
- public int getArity() {
- return 1;
- }
-
- @Override
- public String getName() {
- return "NOT";
- }
- }
-
- public static class Xor extends Function {
- @Override
- public Boolean run(Connection... connections) {
- if (connections.length < 2) {
- throw new InvalidArgumentsException("Not enough connections were given.");
- } else {
- Boolean arg1 = ((Boolean) connections[0].getValue());
- Boolean arg2 = ((Boolean) connections[1].getValue());
- Boolean result = arg1 ^ arg2;
-
- return result;
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "XOR";
- }
- }
-
- public static class Nand extends Function {
- @Override
- public Boolean run(Connection... connections) {
- if (connections.length < 2) {
- throw new InvalidArgumentsException("Not enough connections were given.");
- } else {
- Boolean arg1 = ((Boolean) connections[0].getValue());
- Boolean arg2 = ((Boolean) connections[1].getValue());
- Boolean result = arg1 && arg2;
-
- return !result;
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "NAND";
- }
- }
-
- public static class Nor extends Function {
- @Override
- public Boolean run(Connection... connections) {
- if (connections.length < 2) {
- throw new InvalidArgumentsException("Not enough connections were given.");
- } else {
- Boolean arg1 = ((Boolean) connections[0].getValue());
- Boolean arg2 = ((Boolean) connections[1].getValue());
- Boolean result = arg1 || arg2;
-
- return !result;
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "NOR";
- }
- }
-
- public static class Xnor extends Function {
- @Override
- public Boolean run(Connection... connections) {
- if (connections.length < 2) {
- throw new InvalidArgumentsException("Not enough connections were given.");
- } else {
- Boolean arg1 = ((Boolean) connections[0].getValue());
- Boolean arg2 = ((Boolean) connections[1].getValue());
- Boolean result = arg1 ^ arg2;
-
- return !result;
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "XNOR";
- }
- }
-
-
-
-}
diff --git a/src/jcgp/backend/function/UnsignedInteger.java b/src/jcgp/backend/function/UnsignedInteger.java
new file mode 100644
index 0000000..7feb33f
--- /dev/null
+++ b/src/jcgp/backend/function/UnsignedInteger.java
@@ -0,0 +1,66 @@
+package jcgp.backend.function;
+
+/**
+ * Integer wrapper type for unsigned integer values.
+ * <br><br>
+ * Java offers no support for unsigned types save from
+ * unsigned conversion methods. This class uses those methods
+ * to simulate the unsigned int data type, useful for circuit
+ * truth table encodings.
+ * <br><br>
+ * When a string representation of an unsigned integer is parsed
+ * using Integer.parseUnsignedInt(), an Integer is created using
+ * all 32 bits for unsigned magnitude. The integer however is still
+ * signed and will behave as such for all arithmetic operations.
+ * Bitwise operations can still be performed as they work at the bit
+ * level, making this data type particularly suitable for circuit design.
+ *
+ *
+ * @author Eduardo Pedroni
+ * @see Integer
+ *
+ */
+public class UnsignedInteger {
+
+ private Integer value;
+
+ /**
+ * Makes a new instance of UnsignedInteger with a specified value.
+ *
+ * @param i the value with which to initialise
+ */
+ public UnsignedInteger(int i) {
+ value = new Integer(i);
+ }
+
+ /**
+ * Makes a new instance of UnsignedInteger with a specified value.
+ *
+ * @param i the value with which to initialise
+ */
+ public UnsignedInteger(Integer i) {
+ value = i;
+ }
+
+ /**
+ * Makes a new instance of UnsignedInteger from the string representation
+ * of an unsigned integer.
+ *
+ * @param i the string with which to initialise
+ */
+ public UnsignedInteger(String i) {
+ value = Integer.parseUnsignedInt(i);
+ }
+
+ /**
+ * @return the wrapped Integer object
+ */
+ public Integer get() {
+ return value;
+ }
+
+ @Override
+ public String toString() {
+ return Integer.toUnsignedString(value);
+ }
+}