Helios++
Helios software for LiDAR simulations
UnivarExprTreeStringFactory.h
1 #ifndef _UNIVAREXPRTREESTRINGFACTORY_H_
2 #define _UNIVAREXPRTREESTRINGFACTORY_H_
3 
4 
5 #include <adt/exprtree/UnivarExprTreeNode.h>
6 #include <adt/exprtree/IExprTreeNodeStringFactory.h>
7 #include <util/HeliosException.h>
8 
9 #include <vector>
10 
61 template <typename NumericType>
63  public IExprTreeNodeStringFactory<NumericType, NumericType>{
64 public:
65  // *** CONSTANTS *** //
66  // ******************* //
70  static unsigned int const OP_ADD_BASE_PRIORITY;
74  static unsigned int const OP_MUL_BASE_PRIORITY;
78  static unsigned int const OP_POW_BASE_PRIORITY;
82  static unsigned int const FUN_BASE_PRIORITY;
86  static unsigned int const BRACKET_BASE_PRIORITY;
90  static char const EXPRESSION_CHARS[];
94  static std::string const FUNCTION_NAMES[];
95 
96  // *** SUB-CLASS *** //
97  // ******************* //
102  struct Symbol{
110  std::string str;
111  };
112 
113 
114  // *** ATTRIBUTES *** //
115  // ******************** //
119  unsigned int basePriority = 0;
128  std::vector<UnivarExprTreeNode<NumericType> *> nodes;
132  std::vector<Symbol> ops;
133 
134  // *** CONSTRUCTION / DESTRUCTION *** //
135  // ************************************ //
141  IExprTreeNodeStringFactory<NumericType, NumericType>(),
142  basePriority(0)
143  {}
144  virtual ~UnivarExprTreeStringFactory() = default;
145 
146  // *** EXPRESSION TREE NODE STRING FACTORY INTERFACE *** //
147  // ******************************************************* //
154  std::string const &expr
155  ) override;
156 
157  // *** MAKE METHODS *** //
158  // ********************** //
165  std::string const &expr
166  );
182  virtual void flush();
183 
184  // *** POST-PROCESS METHODS *** //
185  // ****************************** //
192 
193  // *** HANDLE METHODS *** //
194  // ************************ //
200  virtual void handleOp(Symbol const &symbol);
206  virtual void handleFun(Symbol const &symbol);
207 
208 
209  // *** PRIORITY METHODS *** //
210  // ************************** //
218  inline unsigned int calcOpPriority(
219  Symbol const &symbol
220  ){
221  if(symbol.str == "+" || symbol.str == "-")
223  else if(symbol.str == "*" || symbol.str == "/")
225  else if(symbol.str == "^")
227  else if(symbol.str == "(") return basePriority;
228  else if(symbol.str == "atan2") return basePriority + FUN_BASE_PRIORITY;
229  else{
230  throw HeliosException(
231  "UnivarExprTreeStringFactory::calcOpPriority received an "
232  "unexpected operator as input"
233  );
234  }
235  }
242  inline unsigned int calcFunPriority()
243  {return basePriority + FUN_BASE_PRIORITY;}
251  inline unsigned int calcOpenBracketPriority()
261  inline unsigned int calcCloseBracketPriority()
263 
264  // *** UTIL METHODS *** //
265  // ********************** //
272  Symbol nextSymbol(std::string const &expr);
284  std::string prepareExpressionString(std::string const &expr);
295  std::string cleanExpressionString(std::string const &expr);
302  std::string stringFromNumber(
303  NumericType const x,
304  std::streamsize const precision=16
305  );
311  Symbol craftFunSymbol(std::string const &symstr);
320  Symbol craftNumSymbol(std::string const &expr);
328  inline bool isValidOpString(std::string const &opStr){
329  return opStr == "+" || opStr == "-" || opStr == "*" ||
330  opStr == "/" || opStr == "^" || opStr == "(" ||
331  opStr == ")" || opStr == "atan2"
332  ;
333  }
341  inline bool isValidFunString(std::string const &funStr){
342  return funStr == "exp" || funStr == "ln" ||
343  funStr == "sqrt" || funStr == "abs" ||
344  funStr == "cos" || funStr == "sin" ||
345  funStr == "tan" || funStr == "acos" ||
346  funStr == "asin" || funStr == "atan" ||
347  funStr == "cosh" || funStr == "sinh" ||
348  funStr == "tanh";
349  }
350 };
351 
352 #include <adt/exprtree/UnivarExprTreeStringFactory.tpp>
353 
354 #endif
Base class for Helios exceptions.
Definition: HeliosException.h:12
Interface defining the functions that must be provided by any concrete implementation of a factory th...
Definition: IExprTreeNodeStringFactory.h:19
Class implementing a Univariate Expression Tree Node.
Definition: UnivarExprTreeNode.h:27
SymbolType
The different types of element (symbols) supported by the univariate expression tree node.
Definition: UnivarExprTreeNode.h:35
Class implementing a factory that makes univariate expression trees from expressions given as strings...
Definition: UnivarExprTreeStringFactory.h:63
std::string prepareExpressionString(std::string const &expr)
Prepare the expression string so it can be digested by an iterative make process.
UnivarExprTreeStringFactory()
Default constructor for univariate expression tree string factory.
Definition: UnivarExprTreeStringFactory.h:140
std::string cleanExpressionString(std::string const &expr)
Clean the expression string from unnecessary characters.
static std::string const FUNCTION_NAMES[]
Supported functions by name.
Definition: UnivarExprTreeStringFactory.h:94
bool lastReadIsOpenPriorityOrSeparator
True when the last read has been a priority operator or a separator, false otherwise.
Definition: UnivarExprTreeStringFactory.h:124
unsigned int calcFunPriority()
Calculate the corresponding priority considering current base priority and a function.
Definition: UnivarExprTreeStringFactory.h:242
virtual void flush()
Flush the current state of the factory.
static char const EXPRESSION_CHARS[]
Expected characters in expressions.
Definition: UnivarExprTreeStringFactory.h:90
unsigned int calcCloseBracketPriority()
Calculate the corresponding bracket priority considering current base priority and the pop of an open...
Definition: UnivarExprTreeStringFactory.h:261
static unsigned int const OP_ADD_BASE_PRIORITY
The base priority for addition and subtraction operators.
Definition: UnivarExprTreeStringFactory.h:70
Symbol nextSymbol(std::string const &expr)
Obtain the first symbol (token) parsed starting at the character.
void doIPowOptimization(UnivarExprTreeNode< NumericType > *node)
Transform all POW nodes that are either the given node or a child of it into IPOW nodes.
static unsigned int const BRACKET_BASE_PRIORITY
The base priority for brackets.
Definition: UnivarExprTreeStringFactory.h:86
std::vector< UnivarExprTreeNode< NumericType > * > nodes
The nodes at current state (must be used as a stack)
Definition: UnivarExprTreeStringFactory.h:128
unsigned int calcOpenBracketPriority()
Calculate the corresponding priority considering current base priority and the push of an opening bra...
Definition: UnivarExprTreeStringFactory.h:251
virtual void handleFun(Symbol const &symbol)
Handle parsed function.
bool isValidOpString(std::string const &opStr)
Check whether the given string is representing a valid operator (true) or not (false).
Definition: UnivarExprTreeStringFactory.h:328
std::vector< Symbol > ops
The operators at current state (must be used as a stack)
Definition: UnivarExprTreeStringFactory.h:132
static unsigned int const FUN_BASE_PRIORITY
The base priority for functions.
Definition: UnivarExprTreeStringFactory.h:82
static unsigned int const OP_POW_BASE_PRIORITY
The base priority for power operators.
Definition: UnivarExprTreeStringFactory.h:78
IExprTreeNode< NumericType, NumericType > * make(std::string const &expr) override
Make an expression tree from given expression as string.
std::string stringFromNumber(NumericType const x, std::streamsize const precision=16)
Represent the given number as a string.
unsigned int basePriority
The base priority at current state.
Definition: UnivarExprTreeStringFactory.h:119
bool isValidFunString(std::string const &funStr)
Check whether the given string is representing a valid function (true) or not (false).
Definition: UnivarExprTreeStringFactory.h:341
unsigned int calcOpPriority(Symbol const &symbol)
Calculate the corresponding priority considering current base priority and given operator.
Definition: UnivarExprTreeStringFactory.h:218
Symbol craftFunSymbol(std::string const &symstr)
Craft a function symbol from given string.
Symbol craftNumSymbol(std::string const &expr)
Craft the NON-NEGATIVE numeric symbol represented by an arbitrary size string for which the first cha...
static unsigned int const OP_MUL_BASE_PRIORITY
The base priority for product and division operators.
Definition: UnivarExprTreeStringFactory.h:74
virtual void handleOp(Symbol const &symbol)
Handle parsed operator.
virtual IExprTreeNode< NumericType, NumericType > * makeIterative(std::string const &expr)
Iteratively built the corresponding expression tree node from given expression.
UnivarExprTreeStringFactory sub-class used to represent symbols during recursive building.
Definition: UnivarExprTreeStringFactory.h:102
UnivarExprTreeNode< NumericType >::SymbolType type
The symbol type.
Definition: UnivarExprTreeStringFactory.h:106
std::string str
The symbol as a string.
Definition: UnivarExprTreeStringFactory.h:110