Helios++
Helios software for LiDAR simulations
UnivarExprTreeStringFactory< NumericType > Class Template Reference

Class implementing a factory that makes univariate expression trees from expressions given as strings. More...

#include <UnivarExprTreeStringFactory.h>

Inheritance diagram for UnivarExprTreeStringFactory< NumericType >:
Collaboration diagram for UnivarExprTreeStringFactory< NumericType >:

Classes

struct  Symbol
 UnivarExprTreeStringFactory sub-class used to represent symbols during recursive building. More...
 

Public Member Functions

 UnivarExprTreeStringFactory ()
 Default constructor for univariate expression tree string factory.
 
IExprTreeNode< NumericType, NumericType > * make (std::string const &expr) override
 Make an expression tree from given expression as string. More...
 
virtual IExprTreeNode< NumericType, NumericType > * makeIterative (std::string const &expr)
 Iteratively built the corresponding expression tree node from given expression. More...
 
virtual void flush ()
 Flush the current state of the factory. More...
 
void doIPowOptimization (UnivarExprTreeNode< NumericType > *node)
 Transform all POW nodes that are either the given node or a child of it into IPOW nodes. More...
 
virtual void handleOp (Symbol const &symbol)
 Handle parsed operator. More...
 
virtual void handleFun (Symbol const &symbol)
 Handle parsed function. More...
 
unsigned int calcOpPriority (Symbol const &symbol)
 Calculate the corresponding priority considering current base priority and given operator. More...
 
unsigned int calcFunPriority ()
 Calculate the corresponding priority considering current base priority and a function. More...
 
unsigned int calcOpenBracketPriority ()
 Calculate the corresponding priority considering current base priority and the push of an opening bracket. More...
 
unsigned int calcCloseBracketPriority ()
 Calculate the corresponding bracket priority considering current base priority and the pop of an opening bracket (which happens because a close bracket has been read) More...
 
Symbol nextSymbol (std::string const &expr)
 Obtain the first symbol (token) parsed starting at the \(0\) character. More...
 
std::string prepareExpressionString (std::string const &expr)
 Prepare the expression string so it can be digested by an iterative make process. More...
 
std::string cleanExpressionString (std::string const &expr)
 Clean the expression string from unnecessary characters. More...
 
std::string stringFromNumber (NumericType const x, std::streamsize const precision=16)
 Represent the given number as a string. More...
 
Symbol craftFunSymbol (std::string const &symstr)
 Craft a function symbol from given string. More...
 
Symbol craftNumSymbol (std::string const &expr)
 Craft the NON-NEGATIVE numeric symbol represented by an arbitrary size string for which the first character is guaranteed to be the start of the represented number. More...
 
bool isValidOpString (std::string const &opStr)
 Check whether the given string is representing a valid operator (true) or not (false). More...
 
bool isValidFunString (std::string const &funStr)
 Check whether the given string is representing a valid function (true) or not (false). More...
 
- Public Member Functions inherited from IExprTreeNodeStringFactory< NumericType, NumericType >
std::shared_ptr< IExprTreeNode< NumericType, NumericType > > makeShared (std::string const &expr)
 Like IExprTreeNodeStringFactory::make but returning a shared pointer instead of a raw pointer. More...
 

Public Attributes

unsigned int basePriority = 0
 The base priority at current state.
 
bool lastReadIsOpenPriorityOrSeparator = false
 True when the last read has been a priority operator or a separator, false otherwise.
 
std::vector< UnivarExprTreeNode< NumericType > * > nodes
 The nodes at current state (must be used as a stack)
 
std::vector< Symbolops
 The operators at current state (must be used as a stack)
 

Static Public Attributes

static unsigned int const OP_ADD_BASE_PRIORITY
 The base priority for addition and subtraction operators.
 
static unsigned int const OP_MUL_BASE_PRIORITY
 The base priority for product and division operators.
 
static unsigned int const OP_POW_BASE_PRIORITY
 The base priority for power operators.
 
static unsigned int const FUN_BASE_PRIORITY
 The base priority for functions.
 
static unsigned int const BRACKET_BASE_PRIORITY
 The base priority for brackets.
 
static char const EXPRESSION_CHARS []
 Expected characters in expressions.
 
static std::string const FUNCTION_NAMES []
 Supported functions by name.
 

Detailed Description

template<typename NumericType>
class UnivarExprTreeStringFactory< NumericType >

Class implementing a factory that makes univariate expression trees from expressions given as strings.

Author
Alberto M. Esmoris Pena
Version
1.0

The expression strings are expected as infix expressions (infix notation).

Infix expression example: \(a + b\)

Prefix expression example: \(+ a b\)

Postfix expression example: \(a b +\)

The core implementation of the expression tree is based on two sets of logical rules that specify how push and pop operations must be handled.

First, when one of the the following logical rules is satisfied, the algorithm must stop reading and do the corresponding operations to push new trees built from popped operators and trees.

  1. Reading a closing priority operator (e.g., ')')
  2. Reading an operator whose priority is less than or equal to the priority of the operator at the top of the stack of operators
  3. End of reading

Second, when push and pop operations are triggered, they must stop depending on why they have been triggered.

  1. If ')' has been read, then all the elements in the stack of operators must be pop until the corresponding '(' has been reached (inclusive).
  2. If an operator with priority less than or equal to the priority of the operator in the top of the stack has been read, then all the operators in the stack with a priority greater than or equal to read operator must be popped.
  3. If reading has finished, then all the operators in the stack must be consumed and consequently there must be a single tree in the stack of nodes/trees (otherwise the generated expression try will be inconsistent).
Template Parameters
NumericTypeThe numeric type of expression trees to be built by the factory
See also
UnivarExprTreeNode
IExprTreeNodeStringFactory

Member Function Documentation

◆ calcCloseBracketPriority()

template<typename NumericType >
unsigned int UnivarExprTreeStringFactory< NumericType >::calcCloseBracketPriority ( )
inline

Calculate the corresponding bracket priority considering current base priority and the pop of an opening bracket (which happens because a close bracket has been read)

Returns
Calculated priority
See also
UnivarExprTreeStringFactory::basePriority
UnivarExprTreeStringFactory::calcOpenBracketPriority

◆ calcFunPriority()

template<typename NumericType >
unsigned int UnivarExprTreeStringFactory< NumericType >::calcFunPriority ( )
inline

Calculate the corresponding priority considering current base priority and a function.

Returns
Calculated priority
See also
UnivarExprTreeStringFactory::basePriority

◆ calcOpenBracketPriority()

template<typename NumericType >
unsigned int UnivarExprTreeStringFactory< NumericType >::calcOpenBracketPriority ( )
inline

Calculate the corresponding priority considering current base priority and the push of an opening bracket.

Returns
Calculated priority
See also
UnivarExprTreeStringFactory::basePriority
UnivarExprTreeStringFactory::calcCloseBracketPriority

◆ calcOpPriority()

template<typename NumericType >
unsigned int UnivarExprTreeStringFactory< NumericType >::calcOpPriority ( Symbol const &  symbol)
inline

Calculate the corresponding priority considering current base priority and given operator.

Parameters
opGiven operator symbol for which priority must be calculated
Returns
Calculated priority
See also
UnivarExprTreeStringFactory::basePriority

◆ cleanExpressionString()

template<typename NumericType >
std::string UnivarExprTreeStringFactory< NumericType >::cleanExpressionString ( std::string const &  expr)

Clean the expression string from unnecessary characters.

This method is also invoked when UnivarExprTreeStringFactory::prepareExpressionString is invoked.

Parameters
exprThe expression string to be cleaned
Returns
Cleaned expression
See also
UnivarExprTreeStringFactory::prepareEpresssionString

◆ craftFunSymbol()

template<typename NumericType >
Symbol UnivarExprTreeStringFactory< NumericType >::craftFunSymbol ( std::string const &  symstr)

Craft a function symbol from given string.

Parameters
symstrString representation of a function name
Returns
Crafted function symbol

◆ craftNumSymbol()

template<typename NumericType >
Symbol UnivarExprTreeStringFactory< NumericType >::craftNumSymbol ( std::string const &  expr)

Craft the NON-NEGATIVE numeric symbol represented by an arbitrary size string for which the first character is guaranteed to be the start of the represented number.

Parameters
exprString such that there is a substring starting at the first character that represents a number (e.g. "0.567 + 2.1" or "5*3")
Returns
Crafted numeric symbol

◆ doIPowOptimization()

template<typename NumericType >
void UnivarExprTreeStringFactory< NumericType >::doIPowOptimization ( UnivarExprTreeNode< NumericType > *  node)

Transform all POW nodes that are either the given node or a child of it into IPOW nodes.

Parameters
nodeNode to be optimized by using IPOW whenever possible

◆ flush()

template<typename NumericType >
virtual void UnivarExprTreeStringFactory< NumericType >::flush ( )
virtual

Flush the current state of the factory.

The flush operation must be called always that one of the following conditions is satisfied:

  1. A closing priority operator (parentheses) is read
  2. An operator with priority \(\leq\) top operator on UnivarExprTreeStringFactory::ops stack is read
  3. End of parsing
See also
UnivarExprTreeStringFactory::ops

◆ handleFun()

template<typename NumericType >
virtual void UnivarExprTreeStringFactory< NumericType >::handleFun ( Symbol const &  symbol)
virtual

Handle parsed function.

Parameters
symbolSymbol representing the parsed function
See also
UnivarExprTreeStringFactory::Symbol

◆ handleOp()

template<typename NumericType >
virtual void UnivarExprTreeStringFactory< NumericType >::handleOp ( Symbol const &  symbol)
virtual

Handle parsed operator.

Parameters
symbolSymbol representing the parsed operator
See also
UnivarExprTreeStringFactory::Symbol

◆ isValidFunString()

template<typename NumericType >
bool UnivarExprTreeStringFactory< NumericType >::isValidFunString ( std::string const &  funStr)
inline

Check whether the given string is representing a valid function (true) or not (false).

Parameters
funStrThe string to be checked.
Returns
True if given string represents a valid function, false otherwise

◆ isValidOpString()

template<typename NumericType >
bool UnivarExprTreeStringFactory< NumericType >::isValidOpString ( std::string const &  opStr)
inline

Check whether the given string is representing a valid operator (true) or not (false).

Parameters
opStrThe string to be checked.
Returns
True if given string represents a valid operator, false otherwise

◆ make()

template<typename NumericType >
IExprTreeNode<NumericType, NumericType>* UnivarExprTreeStringFactory< NumericType >::make ( std::string const &  expr)
overridevirtual

Make an expression tree from given expression as string.

Parameters
exprThe expression given as a string
Returns
Built univariate expression tree

Implements IExprTreeNodeStringFactory< NumericType, NumericType >.

◆ makeIterative()

template<typename NumericType >
virtual IExprTreeNode<NumericType, NumericType>* UnivarExprTreeStringFactory< NumericType >::makeIterative ( std::string const &  expr)
virtual

Iteratively built the corresponding expression tree node from given expression.

Returns
Iteratively built univariate expression tree

◆ nextSymbol()

template<typename NumericType >
Symbol UnivarExprTreeStringFactory< NumericType >::nextSymbol ( std::string const &  expr)

Obtain the first symbol (token) parsed starting at the \(0\) character.

Parameters
exprThe expression to be parsed to obtain a symbol
Returns
First symbol contained in given expression

◆ prepareExpressionString()

template<typename NumericType >
std::string UnivarExprTreeStringFactory< NumericType >::prepareExpressionString ( std::string const &  expr)

Prepare the expression string so it can be digested by an iterative make process.

This method implies invoking UnivarExprTreeStringFactory::cleanExpressionString always.

Parameters
exprThe expression string to be prepared
Returns
Prepared expression, ready for an iterative make process
See also
UnivarExprTreeStringFactory::cleanExpressionString

◆ stringFromNumber()

template<typename NumericType >
std::string UnivarExprTreeStringFactory< NumericType >::stringFromNumber ( NumericType const  x,
std::streamsize const  precision = 16 
)

Represent the given number as a string.

Parameters
xThe number to be represented as a string
precisionThe amount of decimal digits
Returns
String representation of given number

The documentation for this class was generated from the following file: