return wastage;
}
public void execute(StackInstruction nextInstruction) {
IClusterLevel c1, c2;
int op = nextInstruction.getOperand();
switch (nextInstruction.getType()) {
// PUSH n
// where n is a base cluster ID
// stack = [clusternode $n]:stack
case PUSH:
//don't do anything if the operand has already been put on the stack at some point.
if(seen.contains(op)){
wastage++;
break;
}
if (op == 0) {
Logger.getLogger(StackInterpreter.class.getName()).log(Level.INFO, "Attempt to merge with head of cluster tree aborted");
break;
}
try {
c1 = ClusterUtils.lookupIndexInTree(op, tree);
} catch (UndefinedIndexException e) {
Logger.getLogger(StackInterpreter.class.getName()).log(Level.WARNING, "Operand of PUSH not found", e);
c1 = e.getDefaultNode();
}
seen.add(op);
stack.push(c1);
break;
case CLUSTER:
//take the two elements from the stack
//they are now in a cluster.
//push that cluster back onto the stack.
if (stack.size() < 2) {
//can't cluster less than 2 items. treat this as a PUSH operand
//don't do anything if the operand's been seen.
if(seen.contains(op)){
wastage++;
break;
}
try {
c1 = ClusterUtils.lookupIndexInTree(op, tree);
} catch (UndefinedIndexException e) {
Logger.getLogger(StackInterpreter.class.getName()).log(Level.WARNING, "Operand of PUSH not found", e);
c1 = e.getDefaultNode();
}
stack.push(c1);
seen.add(op);
break;
}
c1 = stack.pop();
c2 = stack.pop();
if (c1.equals(c2)) {
break;
}