ls1-MarDyn
ls1-MarDyn molecular dynamics code
Expression.h
Go to the documentation of this file.
1
6#ifndef EXPRESSION_H
7#define EXPRESSION_H
8
9#include <map>
10#include <set>
11#include <stack>
12#include <list>
13#include <string>
14#include <sstream>
15#include <iostream>
16
17#include <cmath>
18#include <cstdlib> // abs
19
20// Expression ------------------------------------------------------------------------------
22{
23 public:
24 typedef long Tint;
25 typedef double Tfloat;
26
27 enum Evaltype {valtypeNONE, valtypeINT, valtypeFLOAT};
28 typedef enum Evaltype Tvaltype;
29 enum Etraversetype { traversetypePREFIX, traversetypeINFIX, traversetypePOSTFIX };
30 typedef enum Etraversetype Ttraversetype;
31
32 // Value -----------------------------------------------------------------------------------
33 class Value
34 {
35 public:
36 union TUvalue
37 {
38 Tint valInt;
39 Tfloat valFloat;
40 };
41 typedef union TUvalue Tvalue;
42
44 Value() : _type(valtypeNONE) { _value.valInt=0; }
46
50 Value(enum Evaltype type, Tvalue value)
51 : _type(type), _value(value) {}
53
56 //Value(Tint valInt) { _type=valtypeINT; _value.valInt=valInt; }
57 Value(int valInt) { _type=valtypeINT; _value.valInt=Tint(valInt); }
58 Value(long valInt) { _type=valtypeINT; _value.valInt=Tint(valInt); }
60
63 //Value(Tfloat valFloat) { _type=valtypeFLOAT; _value.valFloat=valFloat; }
64 Value(float valFloat) { _type=valtypeFLOAT; _value.valFloat=Tfloat(valFloat); }
65 Value(double valFloat) { _type=valtypeFLOAT; _value.valFloat=Tfloat(valFloat); }
66
68
72 Tvaltype getType() const { return _type; }
74
77 Tvalue getValue() const { return _value; }
79
82 bool isInt() const { return getType()==valtypeINT; }
84
87 bool isFloat() const { return getType()==valtypeFLOAT; }
89
92 Tfloat getValueFloat() const
93 {
94 switch(_type)
95 {
96 case valtypeFLOAT: return _value.valFloat;
97 case valtypeINT: return Tfloat(_value.valInt);
98 default: return 0.;
99 }
100 }
101 operator Tfloat() const { return getValueFloat(); }
103
106 Tint getValueInt() const
107 {
108 switch(_type)
109 {
110 case valtypeINT: return _value.valInt;
111 case valtypeFLOAT: return Tint(_value.valFloat);
112 default: return 0.;
113 }
114 }
115 operator Tint() const { return getValueInt(); }
117
120 void write(std::ostream& ostrm=std::cout) const
121 {
122 if(isInt())
123 ostrm << getValueInt();
124 else if(isFloat())
125 ostrm << getValueFloat();
126 }
127 operator std::string() const
128 {
129 std::ostringstream oss;
130 write(oss);
131 return oss.str();
132 }
133 bool operator==(Value const& v) const;
134 bool operator>(Value const& v) const;
135 bool operator>=(Value const& v) const;
136 bool operator<(Value const& v) const;
137 bool operator<=(Value const& v) const;
138 const Value operator+(Value const& v) const;
139 const Value operator-(Value const& v) const;
140 const Value operator*(Value const& v) const;
141 const Value operator/(Value const& v) const;
142
143 private:
144 Tvaltype _type;
145 Tvalue _value;
146 };
147 // ----------------------------------------------------------------------------------- Value
148
149 class Variable;
150 // VariableGroup ---------------------------------------------------------------------------
158 {
159 public:
160 VariableGroup() : _name(std::string()) {}
161 VariableGroup(const std::string& name) : _name(name) {}
162 const std::string getName() const { return _name; }
163 void addVariable(const Variable* var) { _variables.insert(var); }
164 bool removeVariable(const Variable* var)
165 { if(_variables.count(var)) { _variables.erase(var); return true; } else return false; }
166 unsigned int countVariables() const { return _variables.size(); }
167 operator unsigned int() const { return countVariables(); }
168 private:
169 std::string _name;
170 std::set<const Variable*> _variables;
171 };
172 // --------------------------------------------------------------------------- VariableGroup
173
174 // Variable --------------------------------------------------------------------------------
176 {
177 public:
178 Variable() : _name(std::string()), _value(Value()), _vargrp(NULL) {}
180 : _name(name), _value(Value()), _vargrp(grp) { if(_vargrp) _vargrp->addVariable(this); }
181 template <class T> Variable(const std::string& name, VariableGroup* grp, T val)
182 : _name(name), _value(val), _vargrp(grp) { if(_vargrp) _vargrp->addVariable(this); }
183 ~~Variable() { if(_vargrp) _vargrp->removeVariable(this); }
185
188 const std::string getName() const { return _name; }
190
194 {
195 if(_vargrp)
196 return _vargrp->getName()+":"+_name;
197 else
198 return _name;
199 }
200 Tvaltype getType() const { return _value.getType(); }
201 Value getValue() const { return _value; }
202 Tfloat getValueFloat() const { return _value.getValueFloat(); }
203 operator Tfloat() const { return getValueFloat(); }
204 Tint getValueInt() const { return _value.getValueInt(); }
205 operator Tint() const { return getValueInt(); }
206 bool isInt() const { return _value.isInt(); }
207 bool isFloat() const { return _value.isFloat(); }
208 const VariableGroup* getVariableGroup() const { return _vargrp; }
210
213 template <class T> void setValue(T val) { _value=Value(val); }
214 void write(std::ostream& ostrm=std::cout, bool prtval=false) const
215 {
216 ostrm << getfullName();
217 if(prtval)
218 {
219 ostrm << "{=";
220 _value.write(ostrm);
221 ostrm << "}";
222 }
223 }
224 operator std::string() const
225 {
226 std::ostringstream oss;
227 write(oss);
228 return oss.str();
229 }
230
231 private:
232 std::string _name;
233 Value _value;
234 VariableGroup* _vargrp;
235 };
236 // -------------------------------------------------------------------------------- Variable
237
238 // VariableSet -----------------------------------------------------------------------------
240 {
241 public:
242 VariableSet() {};
243
244 const std::set<const VariableGroup*> getVariableGroupNames() const
245 {
246 std::set<const VariableGroup*> vargroups;
247 for (std::map<std::string,VariableGroup>::const_iterator it=_vargroups.begin(); it!=_vargroups.end(); ++it)
248 vargroups.insert(&it->second);
249 return vargroups;
250 }
251 unsigned int VariableGroupsCount() const { return _vargroups.size(); }
252 bool existVariableGroup(const std::string& name) const { return _vargroups.count(name)>0; }
253 const VariableGroup* getVariableGroup(const std::string& name) const
254 { if(existVariableGroup(name)) return &(_vargroups.find(name)->second); else return NULL; }
255 unsigned int VariableGroupVariablesCount(const std::string& name) const
256 {
257 if(existVariableGroup(name))
258 //return _vargroups[name].countVariables();
259 return (_vargroups.find(name)->second).countVariables();
260 else
261 return 0;
262
263 }
264 unsigned int VariablesCount() const { return _variables.size(); }
265 bool existVariable(const std::string& name) const { return _variables.count(name)>0; }
266 std::set<Variable*> getVariables()
267 {
268 std::set<Variable*> variables;
269 for(std::map<std::string,Variable>::iterator it=_variables.begin(); it!=_variables.end(); ++it)
270 variables.insert(&it->second);
271 return variables;
272 }
273 Variable* getVariable(const std::string& name)
274 {
275 if(existVariable(name))
276 return &_variables[name];
277 else
278 return NULL;
279 }
280 Variable* addVariable(const std::string& name);
281 template <class T> bool setVariable(const std::string& name,T val=0)
282 {
283 Variable* var=getVariable(name);
284 if(var)
285 { // variable already exists
286 var->setValue(val);
287 return false;
288 }
289 else
290 { // create variable
291 var=addVariable(name);
292 var->setValue(val);
293 return true;
294 }
295 }
296 //template <class T> bool setVariable(const char* name,T val=0) { return setVariable(std::string(name),val); }
297 template <class T> bool setVariable(const std::string& vgrpname, const std::string& varname, T val=0)
298 { return setVariable(std::string(vgrpname+":"+varname),val); }
299 //template <class T> bool setVariable(const char* vgrpname, const char* varname, T val=0) { return setVariable(std::string(vgrpname),std::string(varname),val); }
300 bool removeVariable(const std::string& name);
301
302 private:
303 std::map<std::string,Variable> _variables;
304 std::map<std::string,VariableGroup> _vargroups;
305 };
306 // ----------------------------------------------------------------------------- VariableSet
307
308 // Node and derivatives --------------------------------------------------------------------
309
310 // Node ------------------------------------------------------------------------------------
311 class Node
312 {
313 public:
314 Node(Node* child1=NULL, Node* child2=NULL, Node* parent=NULL, short priority=0)
315 : _parent(parent), _priority(priority)
316 {
317 _children[0]=child1;
318 _children[1]=child2;
319 }
320 virtual ~~Node()
321 {
322 if(_children[0]) delete(_children[0]);
323 if(_children[1]) delete(_children[1]);
324 }
325 void setChild1(Node* child1) { _children[0]=child1; }
326 Node* getChild1() const { return _children[0]; }
327 void setChild2(Node* child2) { _children[1]=child2; }
328 Node* getChild2() const { return _children[1]; }
329 void setParent(Node* parent) { _parent=parent; }
330 Node* getParent() const { return _parent; }
331 virtual Tvaltype valueType() const =0;
332 bool isInt() const { return valueType()==valtypeINT; };
333 bool isFloat() const { return valueType()==valtypeFLOAT; };
334 virtual Value evaluate() const =0;
335 virtual Tfloat evaluateFloat() const { return Tfloat(evaluate()); }
336 virtual Tint evaluateInt() const { return Tint(evaluate()); }
337 virtual void write(std::ostream& ostrm) const =0;
338 operator std::string() const
339 {
340 std::ostringstream oss;
341 write(oss);
342 return oss.str();
343 }
344 void write() const { write(std::cout); }
345 void traverse(std::list<const Node*>& nodelist, enum Etraversetype traversetype=traversetypePOSTFIX) const;
346 void writeSubExpr(std::ostream& ostrm=std::cout, enum Etraversetype traversetype=traversetypePOSTFIX, char sep=' ') const;
347
348 protected:
349 Node* _children[2];
350 Node* _parent;
351 short _priority;
352 };
353 // ------------------------------------------------------------------------------------ Node
354
355 // NodeConstant ----------------------------------------------------------------------------
356 class NodeConstant : public Node
357 {
358 public:
359 NodeConstant(Tint val=0, Node* parent=NULL) : Node(NULL,NULL,parent,1), _value(val) {}
360 NodeConstant(Tfloat val, Node* parent=NULL) : Node(NULL,NULL,parent,1), _value(val) {}
361 Tvaltype valueType() const { return _value.getType(); }
362 Value evaluate() const { return _value; }
363 Tfloat evaluateFloat() const { return _value.getValueFloat(); }
364 Tint evaluateInt() const { return _value.getValueInt(); }
365 void write(std::ostream& ostrm) const { _value.write(ostrm); }
366 protected:
367 Value _value;
368 };
369 // ---------------------------------------------------------------------------- NodeConstant
370
371 // NodeVariable ----------------------------------------------------------------------------
372 class NodeVariable : public Node
373 {
374 public:
375 NodeVariable(Variable* var, Node* parent=NULL) : Node(NULL,NULL,parent,1), _var(var) {}
376 Tvaltype valueType() const { if(_var) return _var->getType(); else return valtypeNONE; }
377 Value evaluate() const { if(_var) return _var->getValue(); else return Value(); }
378 Tfloat evaluateFloat() const { if(_var) return _var->getValueFloat(); else return 0.; }
379 Tint evaluateInt() const { if(_var) return _var->getValueInt(); else return 0; }
380 void write(std::ostream& ostrm) const { if(_var) _var->write(ostrm); else ostrm << "undefVar"; }
381 protected:
382 Variable* _var;
383 };
384 // ---------------------------------------------------------------------------- NodeVariable
385
386 // NodeOperation2 --------------------------------------------------------------------------
387 class NodeOperation2 : public Node
388 {
389 public:
390 NodeOperation2(char op, Node* child1, Node* child2, Node* parent=NULL);
391 char op() const { return _operator; }
392 Tvaltype valueType() const
393 {
394 if(_children[0] && _children[1])
395 {
396 if(_children[0]->isInt()&&_children[1]->isInt())
397 return valtypeINT;
398 else
399 return valtypeFLOAT;
400 }
401 else
402 return valtypeNONE;
403 }
404 Value evaluate() const;
405 void write(std::ostream& ostrm) const { ostrm << _operator; }
406 protected:
407 char _operator;
408 };
409 // -------------------------------------------------------------------------- NodeOperation2
410
411 // NodeFunction ----------------------------------------------------------------------------
412 class NodeFunction : public Node
413 {
414 public:
415 enum Efunctype {functypeNONE=0
416 , functypeMarker1Arg // marker for functions with 1 argument ---
417 , functypeABS // absolute value
418 , functypeFLOAT // floating point value
419 , functypeINT // integer value
420 , functypeFLOOR // floor/round down integer value
421 , functypeCEIL // ceiling/round up integer value
422 //, functypeTRUNC // truncated integer value
423 , functypeROUND // rounded integer value
424 , functypeSQRT // square root
425 , functypeLN // natural logarithm
426 , functypeLB // binary logarithm
427 , functypeLG // decimal logarithm
428 , functypeEXP // exponential functions
429 , functypeSIN // sine
430 , functypeCOS // cosine
431 , functypeTAN // tangent
432 , functypeASIN // arc sine
433 , functypeACOS // arc cosine
434 , functypeATAN // arc tangent
435 , functypeMarker2Arg // marker for functions with 2 arguments ---
436 , functypeMIN // minimum of 2 values
437 , functypeMAX // maximum of 2 values
438 , functypeMOD // modulo function
439 , functypePOW // power function
440 , functypeMarkerVarSet // marker for functions using the VariableSet ===
441 , functypeMarkerVarSet1Arg // marker for functions using the VariableSet with 1 argument ---
442 , functypeRCL // recall stored value <id>
443 , functypeMarkerVarSet2Arg // marker for functions using the VariableSet with 2 arguments ---
444 , functypeSTO // store value to <id>
445 };
446
447 static Efunctype functype(const std::string& name);
448
449 NodeFunction(Efunctype func
450 ,Node* child1, Node* child0=NULL, Node* parent=NULL)
451 : Node(child0,child1,parent,0), _functype(func) {}
452 // functions with 1 argument use _children[1]
453 // TODO: probably they should use _children[0] (again), but Node::writeSubExpr needs to be adapted
454 // also NodeFunctionStore
455 Tvaltype valueType() const;
456 Value evaluate() const;
457 void write(std::ostream& ostrm) const;
458
459 protected:
460 enum Efunctype _functype;
461 };
462 // ---------------------------------------------------------------------------- NodeFunction
463
464 // NodeFunctionVarSet ----------------------------------------------------------------------
465 /*
466 Functions with the capability to store and load values from the given VariableSet
467 */
469 {
470 public:
471 // use enum Efunctype defined in NodeFunction
472
473 NodeFunctionVarSet(Efunctype func, VariableSet *variableset
474 ,Node* child1, Node* child0=NULL, Node* parent=NULL)
475 : NodeFunction(func,child1,child0,parent), _variableset(variableset) {}
476 // functions with 1 argument use _children[1] like NodeFunction (see comments there)
477 Tvaltype valueType() const;
478 Value evaluate() const;
479 void write(std::ostream& ostrm) const;
480
481 protected:
482 VariableSet* _variableset;
483 };
484 // ---------------------------------------------------------------------- NodeFunctionVarSet
485
486 // -------------------------------------------------------------------- Node and derivatives
487
488 Expression(const std::string& label=std::string(), VariableSet* varset=NULL)
489 : _rootnode(NULL), _label(label), _variableset(varset), _variablesetcreated(false)
490 {
491 if(!_variableset)
492 {
493 _variableset=new VariableSet();
494 _variablesetcreated=true;
495 }
496 }
497 Expression(const Expression& expr) { *this=expr; }
498 ~~Expression()
499 {
500 clear();
501 if(_variablesetcreated) delete _variableset;
502 }
503
504 void clear() { if(_rootnode) { delete(_rootnode); _rootnode=NULL; } }
505 void setLabel(const std::string& label) { _label=label; }
506 const std::string& getLabel() const { return _label; }
507
508 void initializeRPN(const std::string& exprstr, bool genlabel=true);
509
510 Expression& operator=(const Expression& rhs )
511 {
512 _label=rhs._label;
513 _variableset=rhs._variableset;
514 _variablesetcreated=false;
515 std::ostringstream oss;
516 rhs.writeExpr(oss,traversetypePOSTFIX);
517 _rootnode=NULL; // prevents initializeRPN to clear a non-existant tree
518 initializeRPN(oss.str());
519 return *this;
520 }
521
522 bool isEmpty() const { return _rootnode==NULL; }
523 bool isInt() const { if(_rootnode) return _rootnode->isInt(); else return false; }
524 bool isFloat() const { if(_rootnode) return _rootnode->isFloat(); else return false; }
525 Tfloat evaluateFloat() const
526 {
527 if(_rootnode)
528 return _rootnode->evaluateFloat();
529 else
530 return 0.;
531 }
532 Tint evaluateInt() const
533 {
534 if(_rootnode)
535 return _rootnode->evaluateInt();
536 else
537 return 0;
538 }
539
540 VariableSet* getVariableSet() { return _variableset; }
541 Variable* getVariable(const std::string& name) { return _variableset->getVariable(name); }
542 unsigned int VariablesCount() const { return _variableset->VariablesCount(); }
543 bool existVariable(const std::string& name) const { return _variableset->existVariable(name); }
544 unsigned int VariableGroupsCount() const { return _variableset->VariableGroupsCount(); }
545 bool existVariableGroup(const std::string& name) const { return _variableset->existVariableGroup(name); }
546
547 void writeExpr(std::ostream& ostrm=std::cout, enum Etraversetype traversetype=traversetypePOSTFIX, char sep=' ') const
548 {
549 if(_rootnode) _rootnode->writeSubExpr(ostrm,traversetype,sep);
550 }
551 operator std::string() const
552 {
553 std::ostringstream oss;
554 writeExpr(oss,traversetypeINFIX,0);
555 return oss.str();
556 }
557 void genLabel()
558 {
559 _label=operator std::string();
560 //_label=static_cast<std::string>(*this);
561 }
562
563 protected:
564 Node* _rootnode;
565 std::string _label;
566 VariableSet* _variableset;
567 bool _variablesetcreated;
568};
569// ------------------------------------------------------------------------------ Expression
570
571
572inline std::ostream& operator << (std::ostream& ostrm, const Expression::Value& v)
573{
574 v.write(ostrm);
575 return ostrm;
576}
577
578inline std::ostream& operator << (std::ostream& ostrm, const Expression::Variable& v)
579{
580 v.write(ostrm);
581 return ostrm;
582}
583
584inline std::ostream& operator << (std::ostream& ostrm, const Expression::Node& n)
585{
586 n.write(ostrm);
587 return ostrm;
588}
589
590inline std::ostream& operator << (std::ostream& ostrm, const Expression& e)
591{
592 e.writeExpr(ostrm,Expression::traversetypeINFIX,0);
593 return ostrm;
594}
595
596
597#endif
Definition: Expression.h:357
Definition: Expression.h:469
Definition: Expression.h:413
Definition: Expression.h:388
Definition: Expression.h:373
Definition: Expression.h:312
Definition: Expression.h:34
Value()
Constructor.
Definition: Expression.h:44
Value(enum Evaltype type, Tvalue value)
Constructor.
Definition: Expression.h:50
bool isFloat() const
is the value an floating point value?
Definition: Expression.h:87
void write(std::ostream &ostrm=std::cout) const
write value to stream
Definition: Expression.h:120
Tvaltype getType() const
get value type
Definition: Expression.h:72
bool isInt() const
is the value an integer value?
Definition: Expression.h:82
Value(float valFloat)
Constructor.
Definition: Expression.h:64
Tint getValueInt() const
get value as integer number
Definition: Expression.h:106
Tvalue getValue() const
get value
Definition: Expression.h:77
Value(int valInt)
Constructor.
Definition: Expression.h:57
Tfloat getValueFloat() const
get value as floating point number
Definition: Expression.h:92
Definition: Expression.h:158
Definition: Expression.h:240
Definition: Expression.h:176
const std::string getfullName() const
full variable name
Definition: Expression.h:193
void setValue(T val)
set value
Definition: Expression.h:213
const std::string getName() const
variable name
Definition: Expression.h:188
Definition: Expression.h:22
Enumeration class corresponding to the type schema type.
Definition: vtk-unstructured.h:1746
::xsd::cxx::tree::name< char, token > name
C++ type corresponding to the Name XML Schema built-in type.
Definition: vtk-punstructured.h:288
::xsd::cxx::tree::string< char, simple_type > string
C++ type corresponding to the string XML Schema built-in type.
Definition: vtk-punstructured.h:270
Definition: Expression.h:37