Added a megabuf function to the expression parser, which works like gmegabuf, except that there is one buffer per Expression instance.

This commit is contained in:
TomyLobo 2011-11-30 05:27:11 +01:00
parent c6518a9243
commit 9cdac001e3
2 changed files with 23 additions and 5 deletions

View File

@ -63,6 +63,7 @@ public class Expression {
private final Map<String, RValue> variables = new HashMap<String, RValue>(); private final Map<String, RValue> variables = new HashMap<String, RValue>();
private final String[] variableNames; private final String[] variableNames;
private RValue root; private RValue root;
private final Map<Integer, double[]> megabuf = new HashMap<Integer, double[]>();
public static Expression compile(String expression, String... variableNames) throws ExpressionException { public static Expression compile(String expression, String... variableNames) throws ExpressionException {
return new Expression(expression, variableNames); return new Expression(expression, variableNames);
@ -151,4 +152,8 @@ public class Expression {
instance.set(null); instance.set(null);
} }
} }
public Map<Integer, double[]> getMegabuf() {
return megabuf;
}
} }

View File

@ -26,6 +26,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.runtime.Function.Dynamic; import com.sk89q.worldedit.expression.runtime.Function.Dynamic;
/** /**
@ -272,10 +273,10 @@ public final class Functions {
private static final Map<Integer, double[]> gmegabuf = new HashMap<Integer, double[]>(); private static final Map<Integer, double[]> gmegabuf = new HashMap<Integer, double[]>();
private static double[] getSubBuffer(Integer key) { private static double[] getSubBuffer(Map<Integer, double[]> megabuf, Integer key) {
double[] ret = gmegabuf.get(key); double[] ret = megabuf.get(key);
if (ret == null) { if (ret == null) {
gmegabuf.put(key, ret = new double[1024]); megabuf.put(key, ret = new double[1024]);
} }
return ret; return ret;
} }
@ -283,13 +284,25 @@ public final class Functions {
@Dynamic @Dynamic
public static final double gmegabuf(RValue index) throws EvaluationException { public static final double gmegabuf(RValue index) throws EvaluationException {
final int intIndex = (int) index.getValue(); final int intIndex = (int) index.getValue();
return getSubBuffer(intIndex & ~1023)[intIndex & 1023]; return getSubBuffer(gmegabuf, intIndex & ~1023)[intIndex & 1023];
} }
@Dynamic @Dynamic
public static final double gmegabuf(RValue index, double value) throws EvaluationException { public static final double gmegabuf(RValue index, double value) throws EvaluationException {
final int intIndex = (int) index.getValue(); final int intIndex = (int) index.getValue();
return getSubBuffer(intIndex & ~1023)[intIndex & 1023] = value; return getSubBuffer(gmegabuf, intIndex & ~1023)[intIndex & 1023] = value;
}
@Dynamic
public static final double megabuf(RValue index) throws EvaluationException {
final int intIndex = (int) index.getValue();
return getSubBuffer(Expression.getInstance().getMegabuf(), intIndex & ~1023)[intIndex & 1023];
}
@Dynamic
public static final double megabuf(RValue index, double value) throws EvaluationException {
final int intIndex = (int) index.getValue();
return getSubBuffer(Expression.getInstance().getMegabuf(), intIndex & ~1023)[intIndex & 1023] = value;
} }