Salome HOME
Boost of expression evaluator DataArrayDouble::applyFunc* + DataArrayDouble::applyFun...
[modules/med.git] / src / INTERP_KERNEL / ExprEval / InterpKernelExprParser.hxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #ifndef __INTERPKERNELEXPRPARSER_HXX__
22 #define __INTERPKERNELEXPRPARSER_HXX__
23
24 #include "INTERPKERNELDefines.hxx"
25 #include "InterpKernelUnit.hxx"
26 #include "InterpKernelException.hxx"
27 #include "InterpKernelFunction.hxx"
28
29 #include <string>
30 #include <list>
31 #include <map>
32 #include <set>
33
34 namespace INTERP_KERNEL
35 {
36   class ValueDouble;
37
38   class LeafExpr
39   {
40   public:
41     INTERPKERNEL_EXPORT virtual ~LeafExpr();
42     INTERPKERNEL_EXPORT virtual double getDoubleValue() const = 0;
43     INTERPKERNEL_EXPORT virtual void fillValue(Value *val) const = 0;
44     INTERPKERNEL_EXPORT virtual void compileX86(std::vector<std::string>& ass) const = 0;
45     INTERPKERNEL_EXPORT virtual void compileX86_64(std::vector<std::string>& ass) const = 0;
46     INTERPKERNEL_EXPORT virtual void replaceValues(const std::vector<double>& valuesInExpr) = 0;
47     INTERPKERNEL_EXPORT virtual LeafExpr *deepCpy() const = 0;
48     INTERPKERNEL_EXPORT static LeafExpr *buildInstanceFrom(const std::string& expr);
49   };
50
51   class LeafExprVal : public LeafExpr
52   {
53   public:
54     INTERPKERNEL_EXPORT LeafExprVal(double value);
55     INTERPKERNEL_EXPORT ~LeafExprVal();
56     INTERPKERNEL_EXPORT double getDoubleValue() const;
57     INTERPKERNEL_EXPORT void compileX86(std::vector<std::string>& ass) const;
58     INTERPKERNEL_EXPORT void compileX86_64(std::vector<std::string>& ass) const;
59     INTERPKERNEL_EXPORT void fillValue(Value *val) const;
60     INTERPKERNEL_EXPORT void replaceValues(const std::vector<double>& valuesInExpr);
61     INTERPKERNEL_EXPORT LeafExprVal *deepCpy() const;
62   private:
63     double _value;
64   };
65
66   class LeafExprVar : public LeafExpr
67   {
68   public:
69     INTERPKERNEL_EXPORT LeafExprVar(const LeafExprVar& other):_fast_pos(other._fast_pos),_ref_pos(other._ref_pos),_var_name(other._var_name),_val(other._val) { }
70     INTERPKERNEL_EXPORT LeafExprVar(const std::string& var);
71     INTERPKERNEL_EXPORT ~LeafExprVar();
72     INTERPKERNEL_EXPORT double getDoubleValue() const;
73     INTERPKERNEL_EXPORT void compileX86(std::vector<std::string>& ass) const;
74     INTERPKERNEL_EXPORT void compileX86_64(std::vector<std::string>& ass) const;
75     INTERPKERNEL_EXPORT void fillValue(Value *val) const;
76     INTERPKERNEL_EXPORT std::string getVar() const { return _var_name; }
77     INTERPKERNEL_EXPORT void prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const;
78     INTERPKERNEL_EXPORT void prepareExprEvaluationDouble(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo, int refPos, const double *ptOfInputStart, const double *ptOfInputEnd) const;
79     INTERPKERNEL_EXPORT void prepareExprEvaluationVec() const;
80     INTERPKERNEL_EXPORT void replaceValues(const std::vector<double>& valuesInExpr);
81     INTERPKERNEL_EXPORT static bool isRecognizedKeyVar(const std::string& var, int& pos);
82     INTERPKERNEL_EXPORT LeafExprVar *deepCpy() const;
83   public:
84     static const char END_OF_RECOGNIZED_VAR[];
85   private:
86     mutable int _fast_pos;
87     mutable int _ref_pos;
88     std::string _var_name;
89     mutable const double *_val;
90   };
91
92   class ExprParserOfEval
93   {
94   public:
95     ExprParserOfEval():_leaf(0) { }
96     ExprParserOfEval(LeafExpr *leaf, const std::vector<ExprParserOfEval>& subParts, const std::vector<Function *>& funcs):_leaf(leaf),_sub_parts(subParts),_funcs(funcs) { }
97     void evaluateDoubleInternal(std::vector<double>& stck) const
98     {
99       if(_leaf)
100         stck.push_back(_leaf->getDoubleValue());
101       else
102         for(std::vector<ExprParserOfEval>::const_iterator iter=_sub_parts.begin();iter!=_sub_parts.end();iter++)
103           (*iter).evaluateDoubleInternal(stck);
104       for(std::vector<Function *>::const_iterator iter3=_funcs.begin();iter3!=_funcs.end();iter3++)
105         (*iter3)->operateStackOfDouble(stck);
106     }
107     void evaluateDoubleInternalSafe(std::vector<double>& stck) const
108     {
109       if(_leaf)
110         stck.push_back(_leaf->getDoubleValue());
111       else
112         for(std::vector<ExprParserOfEval>::const_iterator iter=_sub_parts.begin();iter!=_sub_parts.end();iter++)
113           (*iter).evaluateDoubleInternalSafe(stck);
114       for(std::vector<Function *>::const_iterator iter3=_funcs.begin();iter3!=_funcs.end();iter3++)
115         (*iter3)->operateStackOfDoubleSafe(stck);
116     }
117     void clearSortedMemory();
118     void sortMemory();
119   private:
120     LeafExpr *_leaf;
121     std::vector<ExprParserOfEval> _sub_parts;
122     std::vector<Function *> _funcs;
123   };
124
125   class ExprParser
126   {
127   public:
128     INTERPKERNEL_EXPORT ExprParser(const std::string& expr, ExprParser *father=0);
129     INTERPKERNEL_EXPORT ExprParser(const char *expr, int lgth, ExprParser *father=0);
130     INTERPKERNEL_EXPORT ~ExprParser();
131     INTERPKERNEL_EXPORT void parse();
132     INTERPKERNEL_EXPORT bool isParsingSuccessfull() const { return _is_parsing_ok; }
133     INTERPKERNEL_EXPORT double evaluate() const;
134     INTERPKERNEL_EXPORT DecompositionInUnitBase evaluateUnit() const;
135     INTERPKERNEL_EXPORT void prepareExprEvaluation(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo) const;
136     INTERPKERNEL_EXPORT void prepareExprEvaluationDouble(const std::vector<std::string>& vars, int nbOfCompo, int targetNbOfCompo, int refPos, const double *ptOfInputStart, const double *ptOfInputEnd) const;
137     INTERPKERNEL_EXPORT void prepareFastEvaluator() const;
138     INTERPKERNEL_EXPORT void prepareExprEvaluationVec() const;
139     INTERPKERNEL_EXPORT double evaluateDouble() const;
140     INTERPKERNEL_EXPORT void evaluateDoubleInternal(std::vector<double>& stck) const { _for_eval.evaluateDoubleInternal(stck); }
141     INTERPKERNEL_EXPORT void evaluateDoubleInternalSafe(std::vector<double>& stck) const { _for_eval.evaluateDoubleInternalSafe(stck); }
142     INTERPKERNEL_EXPORT void checkForEvaluation() const;
143     INTERPKERNEL_EXPORT void evaluateExpr(int szOfOutParam, const double *inParam, double *outParam) const;
144     INTERPKERNEL_EXPORT void getSetOfVars(std::set<std::string>& vars) const;
145     INTERPKERNEL_EXPORT void getTrueSetOfVars(std::set<std::string>& vars) const;
146     //
147     INTERPKERNEL_EXPORT char *compileX86() const;
148     INTERPKERNEL_EXPORT char *compileX86_64() const;
149     INTERPKERNEL_EXPORT void compileX86LowLev(std::vector<std::string>& ass) const;
150     INTERPKERNEL_EXPORT void compileX86_64LowLev(std::vector<std::string>& ass) const;
151     INTERPKERNEL_EXPORT int getStackSizeToPlayX86(const ExprParser *asker) const;
152     //
153     INTERPKERNEL_EXPORT static std::string buildStringFromFortran(const char *expr, int lgth);
154     INTERPKERNEL_EXPORT static std::string deleteWhiteSpaces(const std::string& expr);
155   private:
156     Value *evaluateLowLev(Value *valGen) const;
157     void reverseThis();
158     ExprParserOfEval convertMeTo() const;
159   private:
160     void prepareExprEvaluationVecLowLev() const;
161     bool tryToInterpALeaf();
162     void parseUnaryFunc();
163     void parseForCmp();
164     void parseForAddMin();
165     void parseForMulDiv();
166     void parseForPow();
167     void parseDeeper();
168     bool simplify();
169     void releaseFunctions();
170     void checkBracketsParity() const;
171     void fillValuesInExpr(std::vector<double>& valuesInExpr);
172     void replaceValues(const std::vector<double>& valuesInExpr);
173     static double ReplaceAndTraduce(std::string& expr, int id, std::size_t bg, std::size_t end, int& delta);
174     static std::size_t FindCorrespondingOpenBracket(const std::string& expr, std::size_t posOfCloseBracket);
175     static void LocateError(std::ostream& stringToDisp, const std::string& srcOfErr, int posOfErr);
176   private:
177     ExprParser *_father;
178     bool _is_parsed;
179     LeafExpr *_leaf;
180     bool _is_parsing_ok;
181     std::string _expr;
182     mutable ExprParserOfEval _for_eval;
183     std::vector<ExprParser> _sub_expr;
184     std::vector<Function *> _func_btw_sub_expr;
185   private:
186     static const int MAX_X86_FP_ST=8;
187     static const char WHITE_SPACES[];
188     static const char EXPR_PARSE_ERR_MSG[];
189   };
190 }
191
192 #endif