mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-10-25 21:40:08 +00:00
cache UserDefinedFunctions
This commit is contained in:
1 parent
a11e02a84a
commit
414c0b8a38
2 files changed
+36
-13
No files matched your search
+14
@@ -13,6 +13,7 @@ import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;
|
|||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import com.dfsek.terra.api.config.meta.Meta;
|
import com.dfsek.terra.api.config.meta.Meta;
|
||||||
|
|
||||||
@@ -45,4 +46,17 @@ public class FunctionTemplate implements ObjectTemplate<FunctionTemplate> {
|
|||||||
public LinkedHashMap<String, FunctionTemplate> getFunctions() {
|
public LinkedHashMap<String, FunctionTemplate> getFunctions() {
|
||||||
return functions;
|
return functions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if(this == o) return true;
|
||||||
|
if(o == null || getClass() != o.getClass()) return false;
|
||||||
|
FunctionTemplate that = (FunctionTemplate) o;
|
||||||
|
return args.equals(that.args) && function.equals(that.function) && functions.equals(that.functions);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(args, function, functions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+22
-13
@@ -15,6 +15,8 @@ import com.dfsek.paralithic.functions.dynamic.Context;
|
|||||||
import com.dfsek.paralithic.functions.dynamic.DynamicFunction;
|
import com.dfsek.paralithic.functions.dynamic.DynamicFunction;
|
||||||
import com.dfsek.paralithic.node.Statefulness;
|
import com.dfsek.paralithic.node.Statefulness;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import com.dfsek.terra.addons.noise.config.templates.FunctionTemplate;
|
import com.dfsek.terra.addons.noise.config.templates.FunctionTemplate;
|
||||||
@@ -24,26 +26,33 @@ public class UserDefinedFunction implements DynamicFunction {
|
|||||||
private final Expression expression;
|
private final Expression expression;
|
||||||
private final int args;
|
private final int args;
|
||||||
|
|
||||||
|
private static final Map<FunctionTemplate, UserDefinedFunction> CACHE = new HashMap<>();
|
||||||
|
|
||||||
protected UserDefinedFunction(Expression expression, int args) {
|
protected UserDefinedFunction(Expression expression, int args) {
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
this.args = args;
|
this.args = args;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UserDefinedFunction newInstance(FunctionTemplate template) throws ParseException {
|
public static UserDefinedFunction newInstance(FunctionTemplate template) throws ParseException {
|
||||||
Parser parser = new Parser();
|
UserDefinedFunction function = CACHE.get(template);
|
||||||
Scope parent = new Scope();
|
if(function == null) {
|
||||||
|
Parser parser = new Parser();
|
||||||
Scope functionScope = new Scope().withParent(parent);
|
Scope parent = new Scope();
|
||||||
|
|
||||||
template.getArgs().forEach(functionScope::addInvocationVariable);
|
Scope functionScope = new Scope().withParent(parent);
|
||||||
|
|
||||||
for(Entry<String, FunctionTemplate> entry : template.getFunctions().entrySet()) {
|
template.getArgs().forEach(functionScope::addInvocationVariable);
|
||||||
String id = entry.getKey();
|
|
||||||
FunctionTemplate nest = entry.getValue();
|
for(Entry<String, FunctionTemplate> entry : template.getFunctions().entrySet()) {
|
||||||
parser.registerFunction(id, newInstance(nest));
|
String id = entry.getKey();
|
||||||
|
FunctionTemplate nest = entry.getValue();
|
||||||
|
parser.registerFunction(id, newInstance(nest));
|
||||||
|
}
|
||||||
|
|
||||||
|
function = new UserDefinedFunction(parser.parse(template.getFunction(), functionScope), template.getArgs().size());
|
||||||
|
CACHE.put(template, function);
|
||||||
}
|
}
|
||||||
|
return function;
|
||||||
return new UserDefinedFunction(parser.parse(template.getFunction(), functionScope), template.getArgs().size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in new issue
Block a user