Optimization. Define variable if only exist in formula.

--HG--
branch : develop
This commit is contained in:
dismine 2014-07-29 18:15:18 +03:00
parent 1a4953ad73
commit c0debc16e0
2 changed files with 45 additions and 105 deletions

View File

@ -52,13 +52,10 @@ int Calculator::iVal = -1;
* @param data pointer to a variable container. * @param data pointer to a variable container.
*/ */
Calculator::Calculator(const VContainer *data) Calculator::Calculator(const VContainer *data)
:QmuParser(), vVarVal(nullptr) :QmuParser(), vVarVal(nullptr), data(data)
{ {
InitCharacterSets(); InitCharacterSets();
// Add variables
InitVariables(data);
// Add unary operators // Add unary operators
DefinePostfixOprt(cm_Oprt, CmUnit); DefinePostfixOprt(cm_Oprt, CmUnit);
DefinePostfixOprt(mm_Oprt, MmUnit); DefinePostfixOprt(mm_Oprt, MmUnit);
@ -85,7 +82,7 @@ Calculator::Calculator(const VContainer *data)
* @param fromUser true if we parse formula from user * @param fromUser true if we parse formula from user
*/ */
Calculator::Calculator(const QString &formula, bool fromUser) Calculator::Calculator(const QString &formula, bool fromUser)
:QmuParser(), vVarVal(nullptr) :QmuParser(), vVarVal(nullptr), data(nullptr)
{ {
InitCharacterSets(); InitCharacterSets();
SetVarFactory(AddVariable, this); SetVarFactory(AddVariable, this);
@ -123,7 +120,7 @@ Calculator::Calculator(const QString &formula, bool fromUser)
} }
SetExpr(formula); SetExpr(formula);
//Need run for making tokens. Don't catch exception here, because it will show us in dialog that formula has error. //Need run for making tokens. Don't catch exception here, because because we want know if formula has error.
Eval(); Eval();
} }
@ -141,126 +138,68 @@ Calculator::~Calculator()
*/ */
qreal Calculator::EvalFormula(const QString &formula) qreal Calculator::EvalFormula(const QString &formula)
{ {
SetVarFactory(AddVariable, this);
SetExpr(formula); SetExpr(formula);
return Eval();
qreal result = 0;
result = Eval();
QMap<int, QString> tokens = this->GetTokens();
if (tokens.isEmpty())
{
return result;
}
// Add variables
InitVariables(data, tokens);
result = Eval();
return result;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void Calculator::InitVariables(const VContainer *data) void Calculator::InitVariables(const VContainer *data, const QMap<int, QString> &tokens)
{ {
int num = 0;
if (qApp->patternType() == MeasurementsType::Standard) if (qApp->patternType() == MeasurementsType::Standard)
{ {
num +=2; vVarVal = new qreal[2];
} }
const QMap<QString, VLengthLine *> lengthLines = data->DataLengthLines(); const QHash<QString, VInternalVariable*> *vars = data->DataVariables();
num += lengthLines.size();
const QMap<QString, VLengthSpline *> lengthSplines = data->DataLengthSplines(); QMap<int, QString>::const_iterator i = tokens.constBegin();
num += lengthSplines.size(); while (i != tokens.constEnd())
const QMap<QString, VLengthArc *> lengthArcs = data->DataLengthArcs();
num += lengthArcs.size();
const QMap<QString, VLineAngle *> lineAngles = data->DataLineAngles();
num += lineAngles.size();
const QMap<QString, VMeasurement*> measurements = data->DataMeasurements();
num += measurements.size();
const QMap<QString, VIncrement*> increments = data->DataIncrements();
num += increments.size();
vVarVal = new qreal[num];
int j = 0;
if (qApp->patternType() == MeasurementsType::Standard)
{ {
vVarVal[j] = data->size(); if (vars->contains(i.value()))
DefineVar(data->SizeName(), &vVarVal[j]);
++j;
vVarVal[j] = data->height();
DefineVar(data->HeightName(), &vVarVal[j]);
++j;
}
{ {
QMap<QString, VLengthLine *>::const_iterator i = lengthLines.constBegin(); VInternalVariable *var = vars->value(i.value());
while (i != lengthLines.constEnd()) if ((qApp->patternType() == MeasurementsType::Standard) &&
{ (var->GetType() == VarType::Measurement || var->GetType() == VarType::Increment))
vVarVal[j] = *i.value()->GetValue();
DefineVar(i.key(), &vVarVal[j]);
++j;
++i;
}
}
{
QMap<QString, VLengthSpline *>::const_iterator i = lengthSplines.constBegin();
while (i != lengthSplines.constEnd())
{
vVarVal[j] = *i.value()->GetValue();
DefineVar(i.key(), &vVarVal[j]);
++j;
++i;
}
}
{
QMap<QString, VLengthArc *>::const_iterator i = lengthArcs.constBegin();
while (i != lengthArcs.constEnd())
{
vVarVal[j] = *i.value()->GetValue();
DefineVar(i.key(), &vVarVal[j]);
++j;
++i;
}
}
{
QMap<QString, VLineAngle *>::const_iterator i = lineAngles.constBegin();
while (i != lineAngles.constEnd())
{
vVarVal[j] = *i.value()->GetValue();
DefineVar(i.key(), &vVarVal[j]);
++j;
++i;
}
}
{
QMap<QString, VMeasurement*>::const_iterator i = measurements.constBegin();
while (i != measurements.constEnd())
{
VMeasurement *m = i.value();
if (qApp->patternType() == MeasurementsType::Standard)
{ {
VVariable *m = data->GetVariable<VVariable *>(i.value());
m->SetValue(data->size(), data->height()); m->SetValue(data->size(), data->height());
} }
vVarVal[j] = *m->GetValue(); DefineVar(i.value(), var->GetValue());
DefineVar(i.key(), &vVarVal[j]);
++j;
++i;
}
} }
{
QMap<QString, VIncrement*>::const_iterator i = increments.constBegin();
while (i != increments.constEnd())
{
VIncrement *incr = i.value();
if (qApp->patternType() == MeasurementsType::Standard) if (qApp->patternType() == MeasurementsType::Standard)
{ {
incr->SetValue(data->size(), data->height()); if (i.value() == data->SizeName())
{
vVarVal[0] = data->size();
DefineVar(data->SizeName(), &vVarVal[0]);
}
if (i.value() == data->HeightName())
{
vVarVal[1] = data->height();
DefineVar(data->HeightName(), &vVarVal[1]);
}
} }
vVarVal[j] = *incr->GetValue();
DefineVar(i.key(), &vVarVal[j]);
++j;
++i; ++i;
} }
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -63,7 +63,8 @@ private:
Q_DISABLE_COPY(Calculator) Q_DISABLE_COPY(Calculator)
qreal *vVarVal; qreal *vVarVal;
static int iVal; static int iVal;
void InitVariables(const VContainer *data); const VContainer *data;
void InitVariables(const VContainer *data, const QMap<int, QString> &tokens);
void InitCharacterSets(); void InitCharacterSets();
static qreal CmUnit(qreal val); static qreal CmUnit(qreal val);
static qreal MmUnit(qreal val); static qreal MmUnit(qreal val);