CMS 3D CMS Logo

formulaBinaryOperatorEvaluator.h
Go to the documentation of this file.
1 #ifndef CommonTools_Utils_formulaBinaryOperatorEvaluator_h
2 #define CommonTools_Utils_formulaBinaryOperatorEvaluator_h
3 // -*- C++ -*-
4 //
5 // Package: CommonTools/Utils
6 // Class : formulaBinaryOperatorEvaluator
7 //
16 //
17 // Original Author: Christopher Jones
18 // Created: Wed, 23 Sep 2015 17:41:33 GMT
19 //
20 
21 // system include files
22 #include <memory>
23 
24 // user include files
25 #include "formulaEvaluatorBase.h"
26 
27 // forward declarations
28 
29 namespace reco {
30  namespace formula {
32  public:
33  BinaryOperatorEvaluatorBase( std::shared_ptr<EvaluatorBase> iLHS,
34  std::shared_ptr<EvaluatorBase> iRHS,
35  Precedence iPrec) :
36  EvaluatorBase(iPrec),
37  m_lhs(iLHS),
38  m_rhs(iRHS) {}
39 
41  EvaluatorBase(iPrec) {}
42 
43  void swapLeftEvaluator(std::shared_ptr<EvaluatorBase>& iNew ) {
44  m_lhs.swap(iNew);
45  }
46 
47  void setLeftEvaluator(std::shared_ptr<EvaluatorBase> iOther) {
48  m_lhs = std::move(iOther);
49  }
50  void setRightEvaluator(std::shared_ptr<EvaluatorBase> iOther) {
51  m_rhs = std::move(iOther);
52  }
53 
54  EvaluatorBase const* lhs() const { return m_lhs.get(); }
55  EvaluatorBase const* rhs() const { return m_rhs.get(); }
56 
57  private:
58  std::shared_ptr<EvaluatorBase> m_lhs;
59  std::shared_ptr<EvaluatorBase> m_rhs;
60  };
61 
62  template<typename Op>
64  {
65 
66  public:
67  BinaryOperatorEvaluator(std::shared_ptr<EvaluatorBase> iLHS,
68  std::shared_ptr<EvaluatorBase> iRHS,
69  Precedence iPrec):
70  BinaryOperatorEvaluatorBase(std::move(iLHS), std::move(iRHS), iPrec) {}
71 
74 
75  // ---------- const member functions ---------------------
76  double evaluate(double const* iVariables, double const* iParameters) const final {
77  return m_operator(lhs()->evaluate(iVariables,iParameters),rhs()->evaluate(iVariables,iParameters));
78  }
79 
80  std::vector<std::string> abstractSyntaxTree() const final {
81  std::vector<std::string> ret;
82  if( lhs()) {
83  ret = shiftAST(lhs()->abstractSyntaxTree());
84  } else {
85  ret.emplace_back(".nullptr");
86  }
87  if(rhs() ){
88  auto child = shiftAST(rhs()->abstractSyntaxTree());
89  for(auto& v: child) {
90  ret.emplace_back(std::move(v));
91  }
92  } else {
93  ret.emplace_back(".nullptr");
94  }
95  ret.emplace(ret.begin(), std::string("op ")+std::to_string(precedence()) );
96  return ret;
97  }
98 
99  private:
101 
103 
104  // ---------- member data --------------------------------
106 
107  };
108  }
109 }
110 
111 
112 #endif
BinaryOperatorEvaluatorBase(std::shared_ptr< EvaluatorBase > iLHS, std::shared_ptr< EvaluatorBase > iRHS, Precedence iPrec)
void setRightEvaluator(std::shared_ptr< EvaluatorBase > iOther)
std::vector< std::string > abstractSyntaxTree() const final
std::vector< std::string > shiftAST(std::vector< std::string > child)
const EvaluatorBase & operator=(const EvaluatorBase &)=delete
void swapLeftEvaluator(std::shared_ptr< EvaluatorBase > &iNew)
virtual std::vector< std::string > abstractSyntaxTree() const =0
virtual double evaluate(double const *iVariables, double const *iParameters) const =0
void setLeftEvaluator(std::shared_ptr< EvaluatorBase > iOther)
double evaluate(double const *iVariables, double const *iParameters) const final
unsigned int precedence() const
fixed size matrix
BinaryOperatorEvaluator(std::shared_ptr< EvaluatorBase > iLHS, std::shared_ptr< EvaluatorBase > iRHS, Precedence iPrec)
def move(src, dest)
Definition: eostools.py:511