package ec.app.semanticGP.func.classification;

import ec.gp.semantic.BooleanSemantics;
import ec.gp.semantic.ByteSemantics;
import ec.gp.semantic.DoubleSemantics;
import ec.gp.semantic.ISemantics;
import ec.gp.semantic.func.TernaryNode;
import library.semantics.BitSet;

/**
 * Implements "if" instruction. First child is condition (must be Boolean instruction(s)), next are choices (numeric instructions!).
 *
 * @author Tomasz Pawlak
 */
public final class If extends TernaryNode<Byte> {

	@Override
	protected ISemantics execute(ISemantics... arguments) {
		BitSet condition = (BitSet) arguments[0].getValue();
		byte[] argument1 = (byte[]) arguments[1].getValue();
		byte[] argument2 = (byte[]) arguments[2].getValue();

		byte[] semantics = new byte[argument1.length];

		for (int i = 0; i < argument1.length; ++i) {
			semantics[i] = condition.get(i) ? argument1[i] : argument2[i];
		}

		return new ByteSemantics(semantics);
	}

	@Override
	public Byte[] invert(Byte output, int missingArgIdx, Byte... restOfArguments) {
		throw new UnsupportedOperationException("not implemented");
	}

	@Override
	public boolean isSymmetric() {
		return false;
	}

	@Override
	public String toString() {
		return "if";
	}
}
