Caching results

This commit is contained in:
Cavallium 2018-10-12 23:20:12 +02:00
parent 494f8e854f
commit b7407886dd
7 changed files with 51 additions and 70 deletions

View File

@ -71,7 +71,7 @@ public interface Function {
* @throws InterruptedException
*/
ObjectArrayList<Function> simplify(Rule rule) throws Error, InterruptedException;
/**
*
* @param context

View File

@ -9,6 +9,8 @@ import it.cavallium.warppi.util.Utils;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public abstract class FunctionDynamic implements Function {
private boolean simplified;
public FunctionDynamic(final MathContext root) {
this.root = root;
functions = new Function[] {};
@ -131,7 +133,7 @@ public abstract class FunctionDynamic implements Function {
@Override
public abstract FunctionDynamic clone();
@Override
public int hashCode() {
return functions.hashCode() + 883 * super.hashCode();

View File

@ -7,7 +7,7 @@ import it.cavallium.warppi.util.Utils;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public abstract class FunctionOperator implements Function {
/**
* Create a new instance of FunctionOperator. The Math Context will be the
* same of <strong>value1</strong>'s.

View File

@ -6,6 +6,8 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public abstract class FunctionSingle implements Function {
private boolean simplified;
/**
* Create a new instance of FunctionSingle. The Math Context will be the
* same of <strong>value</strong>'s.

View File

@ -1,59 +0,0 @@
package it.cavallium.warppi.math.functions;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class EmptyNumber implements Function {
public EmptyNumber(final MathContext root) {
this.root = root;
}
private final MathContext root;
@Override
public ObjectArrayList<Function> simplify(final Rule rule) throws Error, InterruptedException {
return rule.execute(this);
}
@Override
public MathContext getMathContext() {
return root;
}
@Override
public boolean equals(final Object o) {
return o instanceof EmptyNumber;
}
@Override
public Function clone() {
return new EmptyNumber(root);
}
@Override
public Function clone(MathContext c) {
return new EmptyNumber(c);
}
@Override
public Function setParameter(final int index, final Function var) throws IndexOutOfBoundsException {
throw new IndexOutOfBoundsException();
}
@Override
public Function getParameter(final int index) throws IndexOutOfBoundsException {
throw new IndexOutOfBoundsException();
}
@Override
public ObjectArrayList<Block> toBlock(final MathContext context) {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -13,6 +13,7 @@ public class Variable implements Function {
protected char var;
protected final MathContext root;
protected V_TYPE type = V_TYPE.CONSTANT;
private boolean simplified;
public Variable(final MathContext root, final char val, final V_TYPE type) {
this.root = root;

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.math.solver;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import it.cavallium.warppi.Engine;
@ -9,6 +10,9 @@ import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.math.rules.RuleType;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class MathSolver {
@ -17,6 +21,7 @@ public class MathSolver {
private final AtomicInteger stepState = new AtomicInteger(0);
private int stepStateRepetitions = 0;
private int consecutiveNullSteps = 0;
private final Object2ObjectOpenHashMap<Function, ObjectArrayList<Rule>> simplificationCache;
private enum StepState {
_1_CALCULATION, _2_EXPANSION, _3_CALCULATION, _4_REDUCTION
@ -28,6 +33,7 @@ public class MathSolver {
public MathSolver(final Function initialFunction) {
this.initialFunction = initialFunction;
this.simplificationCache = new Object2ObjectOpenHashMap<Function, ObjectArrayList<Rule>>();
}
@SuppressWarnings("unchecked")
@ -214,15 +220,18 @@ public class MathSolver {
for (final Function fnc : fncs) {
boolean didSomething = false;
for (final Rule rule : rules) {
final List<Function> ruleResults = fnc.simplify(rule);
if (ruleResults != null && !ruleResults.isEmpty()) {
if (results == null) {
results = new ObjectArrayList<>();
if (isSimplified(fnc, rule) == false) {
final List<Function> ruleResults = fnc.simplify(rule);
if (ruleResults != null && !ruleResults.isEmpty()) {
if (results == null) {
results = new ObjectArrayList<>();
}
results.addAll(ruleResults);
appliedRules.add(rule);
setSimplified(fnc, rule);
didSomething = true;
break;
}
results.addAll(ruleResults);
appliedRules.add(rule);
didSomething = true;
break;
}
}
if (!didSomething && fncs.size() > 1) {
@ -248,4 +257,30 @@ public class MathSolver {
}
return results;
}
private boolean isSimplified(Function fnc, Rule rule) {
if (simplificationCache.containsKey(fnc)) {
List<Rule> alreadySimplifiedRules = simplificationCache.get(fnc);
if (alreadySimplifiedRules.contains(rule)) {
return true;
} else {
return false;
}
} else {
simplificationCache.put(fnc, new ObjectArrayList<Rule>());
}
return false;
}
private void setSimplified(Function fnc, Rule rule) {
ObjectArrayList<Rule> oar;
if (simplificationCache.containsKey(fnc)) {
oar = new ObjectArrayList<>();
simplificationCache.put(fnc, oar);
} else {
oar = simplificationCache.get(fnc);
if (oar.contains(rule)) return;
}
oar.add(rule);
}
}