Skip to content

Commit

Permalink
Fix precedence error and added additional functions
Browse files Browse the repository at this point in the history
Further tests of expressions from the data base showed that there
were additional ones that couldn't be handled.
  • Loading branch information
Dr15Jones committed Oct 8, 2015
1 parent 75d1227 commit 99529d8
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 82 deletions.
157 changes: 99 additions & 58 deletions CommonTools/Utils/src/FormulaEvaluator.cc

Large diffs are not rendered by default.

50 changes: 34 additions & 16 deletions CommonTools/Utils/src/formulaBinaryOperatorEvaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,51 @@ namespace reco {
namespace formula {
class BinaryOperatorEvaluatorBase : public EvaluatorBase {
public:
BinaryOperatorEvaluatorBase( Precedence iPrec) :
BinaryOperatorEvaluatorBase( std::shared_ptr<EvaluatorBase> iLHS,
std::shared_ptr<EvaluatorBase> iRHS,
Precedence iPrec) :
EvaluatorBase(iPrec),
m_lhs(iLHS),
m_rhs(iRHS) {}

BinaryOperatorEvaluatorBase(Precedence iPrec) :
EvaluatorBase(iPrec) {}
virtual void swapLeftEvaluator(std::unique_ptr<EvaluatorBase>& iNew) = 0;

void swapLeftEvaluator(std::shared_ptr<EvaluatorBase>& iNew ) {
m_lhs.swap(iNew);
}

void setLeftEvaluator(std::shared_ptr<EvaluatorBase> iOther) {
m_lhs = std::move(iOther);
}
void setRightEvaluator(std::shared_ptr<EvaluatorBase> iOther) {
m_rhs = std::move(iOther);
}

EvaluatorBase const* lhs() const { return m_lhs.get(); }
EvaluatorBase const* rhs() const { return m_rhs.get(); }

private:
std::shared_ptr<EvaluatorBase> m_lhs;
std::shared_ptr<EvaluatorBase> m_rhs;
};

template<typename Op>
class BinaryOperatorEvaluator : public BinaryOperatorEvaluatorBase
{

public:
BinaryOperatorEvaluator(std::unique_ptr<EvaluatorBase> iLHS,
std::unique_ptr<EvaluatorBase> iRHS,
BinaryOperatorEvaluator(std::shared_ptr<EvaluatorBase> iLHS,
std::shared_ptr<EvaluatorBase> iRHS,
Precedence iPrec):
BinaryOperatorEvaluatorBase(iPrec),
m_lhs(std::move(iLHS)),
m_rhs(std::move(iRHS)) {
}
BinaryOperatorEvaluatorBase(std::move(iLHS), std::move(iRHS), iPrec) {}

BinaryOperatorEvaluator(Precedence iPrec):
BinaryOperatorEvaluatorBase(iPrec) {}

// ---------- const member functions ---------------------
virtual double evaluate(double const* iVariables, double const* iParameters) const override final {
return m_operator(m_lhs->evaluate(iVariables,iParameters),m_rhs->evaluate(iVariables,iParameters));
}

void swapLeftEvaluator(std::unique_ptr<EvaluatorBase>& iNew ) override final {
m_lhs.swap(iNew);
return m_operator(lhs()->evaluate(iVariables,iParameters),rhs()->evaluate(iVariables,iParameters));
}

private:
Expand All @@ -63,8 +83,6 @@ namespace reco {
const BinaryOperatorEvaluator& operator=(const BinaryOperatorEvaluator&) = delete;

// ---------- member data --------------------------------
std::unique_ptr<EvaluatorBase> m_lhs;
std::unique_ptr<EvaluatorBase> m_rhs;
Op m_operator;

};
Expand Down
4 changes: 2 additions & 2 deletions CommonTools/Utils/src/formulaFunctionOneArgEvaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace reco {

public:
template<typename T>
explicit FunctionOneArgEvaluator(std::unique_ptr<EvaluatorBase> iArg, T iFunc):
explicit FunctionOneArgEvaluator(std::shared_ptr<EvaluatorBase> iArg, T iFunc):
m_arg(std::move(iArg)),
m_function(iFunc) {}

Expand All @@ -49,7 +49,7 @@ namespace reco {
const FunctionOneArgEvaluator& operator=(const FunctionOneArgEvaluator&) = delete;

// ---------- member data --------------------------------
std::unique_ptr<EvaluatorBase> m_arg;
std::shared_ptr<EvaluatorBase> m_arg;
std::function<double(double)> m_function;
};
}
Expand Down
8 changes: 4 additions & 4 deletions CommonTools/Utils/src/formulaFunctionTwoArgsEvaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace reco {

public:
template<typename T>
FunctionTwoArgsEvaluator(std::unique_ptr<EvaluatorBase> iArg1,
std::unique_ptr<EvaluatorBase> iArg2,
FunctionTwoArgsEvaluator(std::shared_ptr<EvaluatorBase> iArg1,
std::shared_ptr<EvaluatorBase> iArg2,
T iFunc):
m_arg1(std::move(iArg1)),
m_arg2(std::move(iArg2)),
Expand All @@ -54,8 +54,8 @@ namespace reco {
const FunctionTwoArgsEvaluator& operator=(const FunctionTwoArgsEvaluator&) = delete;

// ---------- member data --------------------------------
std::unique_ptr<EvaluatorBase> m_arg1;
std::unique_ptr<EvaluatorBase> m_arg2;
std::shared_ptr<EvaluatorBase> m_arg1;
std::shared_ptr<EvaluatorBase> m_arg2;
std::function<double(double,double)> m_function;
};
}
Expand Down
4 changes: 2 additions & 2 deletions CommonTools/Utils/src/formulaUnaryMinusEvaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace reco {
{

public:
explicit UnaryMinusEvaluator(std::unique_ptr<EvaluatorBase> iArg):
explicit UnaryMinusEvaluator(std::shared_ptr<EvaluatorBase> iArg):
EvaluatorBase(Precedence::kUnaryMinusOperator ),
m_arg(std::move(iArg)) {}

Expand All @@ -48,7 +48,7 @@ namespace reco {
const UnaryMinusEvaluator& operator=(const UnaryMinusEvaluator&) = delete;

// ---------- member data --------------------------------
std::unique_ptr<EvaluatorBase> m_arg;
std::shared_ptr<EvaluatorBase> m_arg;
};
}
}
Expand Down
Loading

0 comments on commit 99529d8

Please sign in to comment.