File indexing completed on 2025-12-13 10:28:57
0001 #ifndef ATOOLS_Math_Algebra_Interpreter_H
0002 #define ATOOLS_Math_Algebra_Interpreter_H
0003
0004 #ifdef USING__Calc_only
0005 #include "Tools.H"
0006 #include "Node.H"
0007 #include "Term.H"
0008 #else
0009 #include "ATOOLS/Org/STL_Tools.H"
0010 #include "ATOOLS/Org/Node.H"
0011 #include "ATOOLS/Math/Term.H"
0012 #endif
0013
0014 #include <string>
0015 #include <set>
0016 #include <map>
0017
0018 namespace ATOOLS {
0019
0020 class Algebra_Interpreter;
0021
0022 class Function {
0023 protected:
0024
0025 std::string m_tag;
0026
0027 public:
0028
0029
0030 Function(const std::string &tag);
0031
0032
0033 virtual ~Function();
0034
0035
0036 virtual Term *Evaluate(Algebra_Interpreter *const interpreter,
0037 const std::vector<Term*> &args) const;
0038
0039
0040 inline const std::string &Tag() const { return m_tag; }
0041
0042 };
0043
0044 inline bool operator<(const Function &f1,const Function &f2)
0045 { return f1.Tag()<f2.Tag(); }
0046
0047 class Operator: public Function {
0048 private:
0049
0050 size_t m_priority;
0051 bool m_binary;
0052
0053 public:
0054
0055
0056 inline Operator(const std::string &tag,
0057 const size_t &priority,const bool binary):
0058 Function(tag), m_priority(priority), m_binary(binary) {}
0059
0060
0061 ~Operator();
0062
0063 virtual size_t FindTag(Algebra_Interpreter *const interpreter,
0064 const std::string &expr,const bool fwd,
0065 size_t cpos=std::string::npos) const;
0066
0067
0068 inline const size_t &Priority() const { return m_priority; }
0069 inline bool Binary() const { return m_binary; }
0070
0071 };
0072
0073 class Interpreter_Function {
0074 protected:
0075
0076 Algebra_Interpreter *p_interpreter;
0077
0078 public:
0079
0080
0081 inline Interpreter_Function(Algebra_Interpreter *interpreter):
0082 p_interpreter(interpreter) {}
0083
0084
0085 virtual ~Interpreter_Function();
0086
0087
0088 virtual std::string Interprete(const std::string &expr) = 0;
0089
0090 };
0091
0092 class Tag_Replacer {
0093 public:
0094
0095
0096 virtual ~Tag_Replacer();
0097
0098
0099 std::string &KillBlanks(std::string& expr) const;
0100
0101 virtual std::string ReplaceTags(std::string &expr) const;
0102 virtual Term *ReplaceTags(Term *term) const;
0103
0104 virtual void AssignId(Term *term);
0105
0106 };
0107
0108 class Algebra_Interpreter: public Tag_Replacer {
0109 public:
0110
0111 typedef std::map<std::string,std::string,String_Sort> String_Map;
0112
0113 typedef std::pair<std::string,Function*> Function_Pair;
0114 typedef std::map<std::string,Function*> Function_Map;
0115 typedef std::pair<size_t,Operator*> Operator_Pair;
0116 typedef std::multimap<size_t,Operator*> Operator_Map;
0117
0118 typedef std::map<size_t,Interpreter_Function*> Interpreter_Map;
0119
0120 typedef std::vector<Term*> Term_Vector;
0121
0122 private:
0123
0124 Function_Map m_functions, m_leafs;
0125
0126 static Function_Map s_functions;
0127 static Operator_Map s_operators;
0128
0129 struct Global_Functions {
0130 Global_Functions();
0131 ~Global_Functions();
0132 inline void AddFunction(Function *const f)
0133 { s_functions.insert(Function_Pair(f->Tag(),f)); }
0134 inline void AddOperator(Operator *const b)
0135 { s_operators.insert(Operator_Pair(b->Priority(),b)); }
0136 };
0137
0138 static Global_Functions s_global;
0139
0140 Interpreter_Map m_interpreters;
0141
0142 Tag_Replacer *p_replacer;
0143
0144 String_Map m_tags;
0145 Term_Vector m_terms;
0146
0147 Node<Function*> *p_root;
0148
0149 std::vector<Term_Vector> m_argvs;
0150
0151 void PrintNode(Node<Function*> *const node) const;
0152
0153 void AddArgs(Node<Function*> *const node);
0154
0155 Term *Iterate(Node<Function*> *const node,size_t &n);
0156
0157 public:
0158
0159
0160 Algebra_Interpreter(const bool standard=true);
0161
0162
0163 virtual ~Algebra_Interpreter();
0164
0165
0166 std::string Interprete(const std::string &expr);
0167 std::string Iterate(const std::string &expr);
0168
0169 Term *Calculate();
0170
0171 void AddFunction(Function *const f);
0172
0173 void AddLeaf(Function *const f);
0174 void AddTerm(Term *const t);
0175
0176 std::string ReplaceTags(std::string &expr) const;
0177 Term *ReplaceTags(Term *expr) const;
0178
0179 void AddTag(const std::string &tag,const std::string &value);
0180 void SetTags(const String_Map &tags);
0181
0182 Node<Function*> *ExtractLeaf(const std::string &expr) const;
0183
0184
0185 inline void SetTagReplacer(Tag_Replacer *const replacer)
0186 { p_replacer=replacer; }
0187
0188 inline const Function_Map &Functions() const { return s_functions; }
0189 inline const Operator_Map &Operators() const { return s_operators; }
0190 inline const Function_Map &Locals() const { return m_functions; }
0191
0192 inline Tag_Replacer * TagReplacer() const { return p_replacer; }
0193
0194 inline void PrintEquation() const { PrintNode(p_root); }
0195
0196 };
0197
0198 }
0199
0200 #define DEFINE_FUNCTION(NAME,TAG) \
0201 namespace ATOOLS { class NAME: public Function { \
0202 public: \
0203 NAME(); \
0204 Term *Evaluate(Algebra_Interpreter *const interpreter, \
0205 const std::vector<Term*> &args) const; \
0206 }; } \
0207 NAME::NAME(): Function(TAG) {}
0208
0209 #define DEFINE_UNARY_OPERATOR(NAME,TAG,PRIORITY) \
0210 namespace ATOOLS { class NAME: public Operator { \
0211 public: \
0212 NAME(); \
0213 Term *Evaluate(Algebra_Interpreter *const interpreter, \
0214 const std::vector<Term*> &args) const; \
0215 }; } \
0216 NAME::NAME(): Operator(TAG,PRIORITY,false) {}
0217
0218 #define DEFINE_BINARY_OPERATOR(NAME,TAG,PRIORITY) \
0219 namespace ATOOLS { class NAME: public Operator { \
0220 public: \
0221 NAME(); \
0222 Term *Evaluate(Algebra_Interpreter *const interpreter, \
0223 const std::vector<Term*> &args) const; \
0224 }; } \
0225 NAME::NAME(): Operator(TAG,PRIORITY,true) {}
0226
0227 #define DEFINE_UNARY_BINARY_OPERATOR(NAME,TAG,PRIORITY,TYPE) \
0228 namespace ATOOLS { class NAME: public Operator { \
0229 public: \
0230 NAME(); \
0231 Term *Evaluate(Algebra_Interpreter *const interpreter, \
0232 const std::vector<Term*> &args) const; \
0233 size_t FindTag(Algebra_Interpreter *const interpreter, \
0234 const std::string &expr, \
0235 const bool fwd,size_t cpos) const; \
0236 }; } \
0237 NAME::NAME(): Operator(TAG,PRIORITY,TYPE) {}
0238
0239 #define DEFINE_INTERPRETER_FUNCTION(NAME) \
0240 namespace ATOOLS { class NAME: public Interpreter_Function { \
0241 public: \
0242 inline NAME(Algebra_Interpreter *interpreter): \
0243 Interpreter_Function(interpreter) {} \
0244 std::string Interprete(const std::string &expr); \
0245 }; } \
0246 std::string NAME::Interprete(const std::string &expr)
0247
0248 #endif