3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-11-10 05:20:04 +01:00

Improved Switch's optimizer.

Dieser Commit ist enthalten in:
TomyLobo 2011-12-04 20:49:53 +01:00
Ursprung d55376e8a6
Commit 04a08e7e0c

Datei anzeigen

@ -20,6 +20,7 @@
package com.sk89q.worldedit.expression.runtime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -118,7 +119,50 @@ public class Switch extends Node implements RValue {
@Override
public RValue optimize() throws EvaluationException {
final RValue optimizedParameter = parameter.optimize();
final List<RValue> newSequence = new ArrayList<RValue>();
if (optimizedParameter instanceof Constant) {
final double parameter = optimizedParameter.getValue();
final Integer index = valueMap.get(parameter);
if (index == null) {
return defaultCase == null ? new Constant(getPosition(), 0.0) : defaultCase.optimize();
}
boolean breakDetected = false;
for (int i = index; i < caseStatements.length && !breakDetected ; ++i) {
final RValue invokable = caseStatements[i].optimize();
if (invokable instanceof Sequence) {
for (RValue subInvokable : ((Sequence) invokable).sequence) {
if (subInvokable instanceof Break) {
breakDetected = true;
break;
}
newSequence.add(subInvokable);
}
} else {
newSequence.add(invokable);
}
}
if (defaultCase != null && !breakDetected) {
final RValue invokable = defaultCase.optimize();
if (invokable instanceof Sequence) {
for (RValue subInvokable : ((Sequence) invokable).sequence) {
newSequence.add(subInvokable);
}
} else {
newSequence.add(invokable);
}
}
return new Switch(getPosition(), optimizedParameter, Collections.singletonMap(parameter, 0), newSequence, null);
}
final Map<Double, Integer> newValueMap = new HashMap<Double, Integer>();
Map<Integer, Double> backMap = new HashMap<Integer, Double>();
@ -143,6 +187,6 @@ public class Switch extends Node implements RValue {
}
}
return new Switch(getPosition(), parameter.optimize(), newValueMap, newSequence, defaultCase.optimize());
return new Switch(getPosition(), optimizedParameter, newValueMap, newSequence, defaultCase.optimize());
}
}