aGrUM  0.14.2
gum::Formula Class Reference

Evaluates a string as a algebraic formula. More...

#include <formula.h>

+ Collaboration diagram for gum::Formula:

Public Member Functions

 Formula (const std::string &f)
 Class constructor. More...
 
 Formula (const Formula &source)
 Copy constructor. More...
 
 Formula (Formula &&source)
 Move constructor. More...
 
 ~Formula ()
 Class destructor. More...
 
Constructors and destructor
 Formula (short s)
 Constructor. More...
 
 Formula (unsigned short us)
 Constructor. More...
 
 Formula (int i)
 Constructor. More...
 
 Formula (unsigned int ui)
 Constructor. More...
 
 Formula (long l)
 Constructor. More...
 
 Formula (unsigned long ul)
 Constructor. More...
 
 Formula (long long l)
 Constructor. More...
 
 Formula (unsigned long long ul)
 Constructor. More...
 
 Formula (float f)
 Constructor. More...
 
 Formula (double d)
 Constructor. More...
 
Operators
Formulaoperator= (const Formula &source)
 Copy operator. More...
 
Formulaoperator= (Formula &&source)
 Move operator. More...
 
 operator double () const
 Allows implicit conversion to doubles. More...
 
Accessors & modifiers
HashTable< std::string, double > & variables ()
 Returns the variables used by this gum::Formula. More...
 
const HashTable< std::string, double > & variables () const
 Returns the variables used by this gum::Formula. More...
 
double result () const
 Returns the result of this gum::Formula. More...
 
const std::string & formula () const
 Returns the formula. More...
 
std::string & formula ()
 Returns the formula. More...
 

Friends

class gum::formula::Parser
 

Detailed Description

Evaluates a string as a algebraic formula.

Implementation of the Shunting-yard algorithm to convert infix notation to RPN. The gum::Formula::result() method implements the postfix algorithm to compute the formula result.

Warning
Checking is only done when evaluating the formula !

Definition at line 271 of file formula.h.

Constructor & Destructor Documentation

◆ Formula() [1/13]

gum::Formula::Formula ( short  s)

Constructor.

Definition at line 181 of file formula.cpp.

References __initialise().

181  :
182  __formula(std::to_string(s)), __last_token(FormulaPart()) {
183  GUM_CONSTRUCTOR(Formula);
184  __initialise();
185  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
Formula(short s)
Constructor.
Definition: formula.cpp:181
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:171
+ Here is the call graph for this function:

◆ Formula() [2/13]

gum::Formula::Formula ( unsigned short  us)

Constructor.

Definition at line 187 of file formula.cpp.

References __initialise().

187  :
188  __formula(std::to_string(us)), __last_token(FormulaPart()) {
189  GUM_CONSTRUCTOR(Formula);
190  __initialise();
191  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
Formula(short s)
Constructor.
Definition: formula.cpp:181
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:171
+ Here is the call graph for this function:

◆ Formula() [3/13]

gum::Formula::Formula ( int  i)

Constructor.

Definition at line 193 of file formula.cpp.

References __initialise().

193  :
194  __formula(std::to_string(i)), __last_token(FormulaPart()) {
195  GUM_CONSTRUCTOR(Formula);
196  __initialise();
197  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
Formula(short s)
Constructor.
Definition: formula.cpp:181
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:171
+ Here is the call graph for this function:

◆ Formula() [4/13]

gum::Formula::Formula ( unsigned int  ui)

Constructor.

Definition at line 199 of file formula.cpp.

References __initialise().

199  :
200  __formula(std::to_string(ui)), __last_token(FormulaPart()) {
201  GUM_CONSTRUCTOR(Formula);
202  __initialise();
203  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
Formula(short s)
Constructor.
Definition: formula.cpp:181
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:171
+ Here is the call graph for this function:

◆ Formula() [5/13]

gum::Formula::Formula ( long  l)

Constructor.

Definition at line 205 of file formula.cpp.

References __initialise().

205  :
206  __formula(std::to_string(l)), __last_token(FormulaPart()) {
207  GUM_CONSTRUCTOR(Formula);
208  __initialise();
209  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
Formula(short s)
Constructor.
Definition: formula.cpp:181
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:171
+ Here is the call graph for this function:

◆ Formula() [6/13]

gum::Formula::Formula ( unsigned long  ul)

Constructor.

Definition at line 211 of file formula.cpp.

References __initialise().

211  :
212  __formula(std::to_string(ul)), __last_token(FormulaPart()) {
213  GUM_CONSTRUCTOR(Formula);
214  __initialise();
215  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
Formula(short s)
Constructor.
Definition: formula.cpp:181
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:171
+ Here is the call graph for this function:

◆ Formula() [7/13]

gum::Formula::Formula ( long long  l)

Constructor.

Definition at line 217 of file formula.cpp.

References __initialise().

217  :
218  __formula(std::to_string(l)), __last_token(FormulaPart()) {
219  GUM_CONSTRUCTOR(Formula);
220  __initialise();
221  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
Formula(short s)
Constructor.
Definition: formula.cpp:181
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:171
+ Here is the call graph for this function:

◆ Formula() [8/13]

gum::Formula::Formula ( unsigned long long  ul)

Constructor.

Definition at line 223 of file formula.cpp.

References __initialise().

223  :
224  __formula(std::to_string(ul)), __last_token(FormulaPart()) {
225  GUM_CONSTRUCTOR(Formula);
226  __initialise();
227  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
Formula(short s)
Constructor.
Definition: formula.cpp:181
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:171
+ Here is the call graph for this function:

◆ Formula() [9/13]

gum::Formula::Formula ( float  f)

Constructor.

Definition at line 229 of file formula.cpp.

References __initialise().

229  :
230  __formula(std::to_string(f)), __last_token(FormulaPart()) {
231  GUM_CONSTRUCTOR(Formula);
232  __initialise();
233  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
Formula(short s)
Constructor.
Definition: formula.cpp:181
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:171
+ Here is the call graph for this function:

◆ Formula() [10/13]

gum::Formula::Formula ( double  d)

Constructor.

Definition at line 235 of file formula.cpp.

References __initialise().

235  :
236  __formula(std::to_string(d)), __last_token(FormulaPart()) {
237  GUM_CONSTRUCTOR(Formula);
238  __initialise();
239  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
Formula(short s)
Constructor.
Definition: formula.cpp:181
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:171
+ Here is the call graph for this function:

◆ Formula() [11/13]

gum::Formula::Formula ( const std::string &  f)

Class constructor.

Parameters
fAn algebraic formula.

Definition at line 241 of file formula.cpp.

References __initialise().

241  :
242  __formula(f), __last_token(FormulaPart()) {
243  GUM_CONSTRUCTOR(Formula);
244 
245  __initialise();
246  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
Formula(short s)
Constructor.
Definition: formula.cpp:181
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:171
+ Here is the call graph for this function:

◆ Formula() [12/13]

gum::Formula::Formula ( const Formula source)

Copy constructor.

Parameters
sourceThe gum::Formula to copy.

Definition at line 248 of file formula.cpp.

References __initialise().

248  :
249  __formula(source.__formula), __last_token(source.__last_token),
250  __output(source.__output), __stack(source.__stack) {
251  GUM_CONS_CPY(Formula);
252 
253  __initialise();
254  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:454
Formula(short s)
Constructor.
Definition: formula.cpp:181
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:171
std::vector< FormulaPart > __output
The output stack, will contain one value after evaluation.
Definition: formula.h:451
+ Here is the call graph for this function:

◆ Formula() [13/13]

gum::Formula::Formula ( Formula &&  source)

Move constructor.

Parameters
sourceThe gum::Formula to move.

Definition at line 256 of file formula.cpp.

References __parser.

256  :
257  __formula(std::move(source.__formula)),
258  __scanner(std::move(source.__scanner)), __parser(std::move(source.__parser)),
259  __last_token(std::move(source.__last_token)),
260  __output(std::move(source.__output)), __stack(std::move(source.__stack)) {
261  GUM_CONS_CPY(Formula);
262 
263  __parser->formula(this);
264  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
std::unique_ptr< gum::formula::Parser > __parser
The parser used by the formula.
Definition: formula.h:445
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:454
Formula(short s)
Constructor.
Definition: formula.cpp:181
std::vector< FormulaPart > __output
The output stack, will contain one value after evaluation.
Definition: formula.h:451
std::unique_ptr< gum::formula::Scanner > __scanner
The scanner used by the formula.
Definition: formula.h:442

◆ ~Formula()

gum::Formula::~Formula ( )

Class destructor.

Definition at line 266 of file formula.cpp.

266 { GUM_DESTRUCTOR(Formula); }
Formula(short s)
Constructor.
Definition: formula.cpp:181

Member Function Documentation

◆ __finalize()

INLINE void gum::Formula::__finalize ( )
private

Finalize the formula and prepare it for evaluation.

Definition at line 329 of file formula_inl.h.

References GUM_ERROR.

329  {
330  while (!__stack.empty()) {
331  if (__stack.top().character == '(') {
332  GUM_ERROR(OperationNotAllowed, "expecting ')'");
333  }
334 
335  __push_output(__stack.top());
336  __stack.pop();
337  }
338  }
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:454
void __push_output(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:359
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

◆ __initialise()

void gum::Formula::__initialise ( )
private

Initialise the formula scanner and parser.

Definition at line 171 of file formula.cpp.

Referenced by Formula(), and operator=().

171  {
172  auto c_str = (unsigned char*)__formula.c_str();
173  auto scanner = new gum::formula::Scanner(c_str, (int)__formula.size());
174  __scanner = std::unique_ptr< gum::formula::Scanner >(scanner);
175 
176  auto parser = new gum::formula::Parser(scanner);
177  __parser = std::unique_ptr< gum::formula::Parser >(parser);
178  __parser->formula(this);
179  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
std::unique_ptr< gum::formula::Parser > __parser
The parser used by the formula.
Definition: formula.h:445
friend class gum::formula::Parser
Definition: formula.h:272
std::unique_ptr< gum::formula::Scanner > __scanner
The scanner used by the formula.
Definition: formula.h:442
+ Here is the caller graph for this function:

◆ __isUnaryOperator()

INLINE bool gum::Formula::__isUnaryOperator ( char  o)
private

Returns true if o is an unary operator.

Returns
Returns true if o is an unary operator.

Definition at line 265 of file formula_inl.h.

265  {
266  switch (__last_token.type) {
267  case FormulaPart::token_type::OPERATOR:
268  case FormulaPart::token_type::NIL:
269  case FormulaPart::token_type::ARG_SEP: {
270  return o == '-';
271  }
272 
273  case FormulaPart::token_type::PARENTHESIS: {
274  return (o == '-') && (__last_token.character == '(');
275  }
276 
277  default: { return false; }
278  }
279  }
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:66
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76

◆ __popOperator()

INLINE bool gum::Formula::__popOperator ( FormulaPart  o)
private

Pop the operator in the inner formula's stack.

Parameters
oThe operator to pop.
Returns
Returns true if the operator was popped.

Definition at line 236 of file formula_inl.h.

References gum::FormulaPart::isLeftAssociative(), gum::FormulaPart::isRightAssociative(), and gum::FormulaPart::precedence().

236  {
237  if (__stack.empty()
238  || __stack.top().type != FormulaPart::token_type::OPERATOR) {
239  return false;
240  }
241 
242  if (o.isLeftAssociative() && o.precedence() <= __stack.top().precedence()) {
243  return true;
244  }
245 
246  if (o.isRightAssociative() && o.precedence() < __stack.top().precedence()) {
247  return true;
248  }
249 
250  return false;
251  }
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:454
+ Here is the call graph for this function:

◆ __push_comma()

INLINE void gum::Formula::__push_comma ( )
private

Push a comma in the formula.

Definition at line 403 of file formula_inl.h.

References gum::FormulaPart::FormulaPart(), and GUM_ERROR.

403  {
404  while ((!__stack.empty()) && (__stack.top().character != '(')) {
405  __push_output(__stack.top());
406  __stack.pop();
407  }
408 
409  if (__stack.empty() || __stack.top().character != '(') {
410  GUM_ERROR(OperationNotAllowed, "expecting a '('");
411  }
412 
413  __last_token = FormulaPart(FormulaPart::token_type::ARG_SEP, ',');
414  }
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:454
void __push_output(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:359
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:

◆ __push_function()

INLINE void gum::Formula::__push_function ( const std::string &  func)
private

Push a function in the formula.

Parameters
funcThe function to push.

Definition at line 371 of file formula_inl.h.

References GUM_ERROR.

371  {
372  if (func == "exp") {
373  FormulaPart t(FormulaPart::token_type::FUNCTION,
374  FormulaPart::token_function::exp);
375  __push_stack(t);
376 
377  } else if (func == "log") {
378  FormulaPart t(FormulaPart::token_type::FUNCTION,
379  FormulaPart::token_function::log);
380  __push_stack(t);
381 
382  } else if (func == "ln") {
383  FormulaPart t(FormulaPart::token_type::FUNCTION,
384  FormulaPart::token_function::ln);
385  __push_stack(t);
386 
387  } else if (func == "pow") {
388  FormulaPart t(FormulaPart::token_type::FUNCTION,
389  FormulaPart::token_function::pow);
390  __push_stack(t);
391 
392  } else if (func == "sqrt") {
393  FormulaPart t(FormulaPart::token_type::FUNCTION,
394  FormulaPart::token_function::sqrt);
395  __push_stack(t);
396 
397  } else {
398  GUM_ERROR(OperationNotAllowed, "unknown function");
399  }
400  }
void __push_stack(FormulaPart t)
Push the gum::FormulaPart in the stack.
Definition: formula_inl.h:365
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

◆ __push_identifier()

INLINE void gum::Formula::__push_identifier ( const std::string &  ident)
private

Use this if you don't know if ident is a function or a variable.

Definition at line 435 of file formula_inl.h.

References GUM_ERROR.

435  {
436  try {
437  __push_function(ident);
438 
439  } catch (OperationNotAllowed&) {
440  try {
441  __push_variable(ident);
442 
443  } catch (OperationNotAllowed&) {
444  GUM_ERROR(OperationNotAllowed, "unknown identifier");
445  }
446  }
447  }
void __push_function(const std::string &func)
Push a function in the formula.
Definition: formula_inl.h:371
void __push_variable(const std::string &var)
Push a variable in the formula.
Definition: formula_inl.h:425
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

◆ __push_leftParenthesis()

INLINE void gum::Formula::__push_leftParenthesis ( )
private

Push a left parenthesis in the formula.

Definition at line 299 of file formula_inl.h.

299  {
300  FormulaPart t(FormulaPart::token_type::PARENTHESIS, '(');
301  __push_stack(t);
302  }
void __push_stack(FormulaPart t)
Push the gum::FormulaPart in the stack.
Definition: formula_inl.h:365

◆ __push_number()

INLINE void gum::Formula::__push_number ( const double v)
private

Push a number in the formula.

Parameters
vThe number to push.

Definition at line 230 of file formula_inl.h.

230  {
231  FormulaPart t(FormulaPart::token_type::NUMBER, v);
232  __push_output(t);
233  }
void __push_output(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:359

◆ __push_operator() [1/2]

INLINE void gum::Formula::__push_operator ( char  o)
private

Push an operator in the formula.

Parameters
oThe operator to push.

Definition at line 254 of file formula_inl.h.

254  {
255  if (__isUnaryOperator(o)) {
257 
258  } else {
259  FormulaPart t(FormulaPart::token_type::OPERATOR, o);
260  __push_operator(t);
261  }
262  }
bool __isUnaryOperator(char o)
Returns true if o is an unary operator.
Definition: formula_inl.h:265
void __push_operator(char o)
Push an operator in the formula.
Definition: formula_inl.h:254
void __push_unaryOperator(char o)
Push an unary operator.
Definition: formula_inl.h:282

◆ __push_operator() [2/2]

INLINE void gum::Formula::__push_operator ( FormulaPart  t)
private

Push an operator.

Parameters
tThe operator to push.

Definition at line 289 of file formula_inl.h.

289  {
290  while (__popOperator(t)) {
291  __push_output(__stack.top());
292  __stack.pop();
293  }
294 
295  __push_stack(t);
296  }
bool __popOperator(FormulaPart o)
Pop the operator in the inner formula&#39;s stack.
Definition: formula_inl.h:236
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:454
void __push_stack(FormulaPart t)
Push the gum::FormulaPart in the stack.
Definition: formula_inl.h:365
void __push_output(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:359

◆ __push_output()

INLINE void gum::Formula::__push_output ( FormulaPart  t)
private

Push the gum::FormulaPart in the output vector.

Parameters
tThe gum::FormulaPart to push.

Definition at line 359 of file formula_inl.h.

359  {
360  __output.push_back(t);
361  __last_token = t;
362  }
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::vector< FormulaPart > __output
The output stack, will contain one value after evaluation.
Definition: formula.h:451

◆ __push_rightParenthesis()

INLINE void gum::Formula::__push_rightParenthesis ( )
private

Push a right parenthesis in the formula.

Definition at line 305 of file formula_inl.h.

References gum::FormulaPart::FormulaPart(), and GUM_ERROR.

305  {
306  while ((!__stack.empty()) && (__stack.top().character != '(')) {
307  __push_output(__stack.top());
308  __stack.pop();
309  }
310 
311  if (__stack.empty()) {
312  GUM_ERROR(OperationNotAllowed, "expecting '('");
313 
314  } else if (__stack.top().character != '(') {
315  GUM_ERROR(OperationNotAllowed, "expecting '('");
316  }
317 
318  __stack.pop();
319 
320  if ((!__stack.empty())
321  && __stack.top().type == FormulaPart::token_type::FUNCTION) {
322  __push_output(__stack.top());
323  __stack.pop();
324  }
325  __last_token = FormulaPart(FormulaPart::token_type::PARENTHESIS, ')');
326  }
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:454
void __push_output(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:359
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:

◆ __push_stack()

INLINE void gum::Formula::__push_stack ( FormulaPart  t)
private

Push the gum::FormulaPart in the stack.

Parameters
tThe gum::FormulaPart to push.

Definition at line 365 of file formula_inl.h.

365  {
366  __stack.push(t);
367  __last_token = t;
368  }
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:454

◆ __push_unaryOperator()

INLINE void gum::Formula::__push_unaryOperator ( char  o)
private

Push an unary operator.

Parameters
oThe unary operator to push.

Definition at line 282 of file formula_inl.h.

282  {
283  // Only unary operator is the negative sign -
284  FormulaPart t(FormulaPart::token_type::OPERATOR, '_');
285  __push_operator(t);
286  }
void __push_operator(char o)
Push an operator in the formula.
Definition: formula_inl.h:254

◆ __push_variable()

INLINE void gum::Formula::__push_variable ( const std::string &  var)
private

Push a variable in the formula.

Definition at line 425 of file formula_inl.h.

References GUM_ERROR.

425  {
426  if (__variables.exists(var)) {
428 
429  } else {
430  GUM_ERROR(OperationNotAllowed, "unknonw variable");
431  }
432  }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
void __push_number(const double &v)
Push a number in the formula.
Definition: formula_inl.h:230
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
HashTable< std::string, double > __variables
The variables available in this formula.
Definition: formula.h:457

◆ __reduceOperatorOrFunction()

INLINE void gum::Formula::__reduceOperatorOrFunction ( FormulaPart  item,
std::stack< FormulaPart > &  stack 
) const
private

Evaluate an operator or function and push its result.

Parameters
itemThe operator or function to reduce.
stackThe stack to evaluate.

Definition at line 342 of file formula_inl.h.

References gum::FormulaPart::argc(), gum::FormulaPart::eval(), and GUM_ERROR.

Referenced by result().

343  {
344  std::vector< FormulaPart > args;
345 
346  if (stack.size() < item.argc()) {
347  GUM_ERROR(OperationNotAllowed, "not enought inputs ");
348  }
349 
350  while (item.argc() > args.size()) {
351  args.push_back(stack.top());
352  stack.pop();
353  }
354 
355  stack.push(item.eval(args));
356  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ formula() [1/2]

INLINE const std::string & gum::Formula::formula ( ) const

Returns the formula.

Definition at line 224 of file formula_inl.h.

224 { return __formula; }
std::string __formula
The formula to evaluate.
Definition: formula.h:439

◆ formula() [2/2]

INLINE std::string & gum::Formula::formula ( )

Returns the formula.

Definition at line 227 of file formula_inl.h.

227 { return __formula; }
std::string __formula
The formula to evaluate.
Definition: formula.h:439

◆ operator double()

gum::Formula::operator double ( ) const
inlineexplicit

Allows implicit conversion to doubles.

Definition at line 342 of file formula.h.

342 { return result(); }
double result() const
Returns the result of this gum::Formula.
Definition: formula.cpp:295

◆ operator=() [1/2]

Formula & gum::Formula::operator= ( const Formula source)

Copy operator.

Parameters
sourceThe gum::Formula to copy.
Returns
Returns this gum::Formula.

Definition at line 268 of file formula.cpp.

References __formula, __initialise(), __last_token, __output, and __stack.

268  {
269  if (this == &source) { return *this; }
270 
271  __formula = source.__formula;
272  __last_token = source.__last_token;
273  __output = source.__output;
274  __stack = source.__stack;
275 
276  __initialise();
277 
278  return *this;
279  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:454
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:171
std::vector< FormulaPart > __output
The output stack, will contain one value after evaluation.
Definition: formula.h:451
+ Here is the call graph for this function:

◆ operator=() [2/2]

Formula & gum::Formula::operator= ( Formula &&  source)

Move operator.

Parameters
sourceThe gum::Formula to move.
Returns
Returns this gum::Formula.

Definition at line 281 of file formula.cpp.

References __formula, __last_token, __output, __parser, __scanner, and __stack.

281  {
282  if (this == &source) { return *this; }
283 
284  __formula = std::move(source.__formula);
285  __scanner = std::move(source.__scanner);
286  __parser = std::move(source.__parser);
287  __parser->formula(this);
288  __last_token = std::move(source.__last_token);
289  __output = std::move(source.__output);
290  __stack = std::move(source.__stack);
291 
292  return *this;
293  }
std::string __formula
The formula to evaluate.
Definition: formula.h:439
std::unique_ptr< gum::formula::Parser > __parser
The parser used by the formula.
Definition: formula.h:445
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:448
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:454
std::vector< FormulaPart > __output
The output stack, will contain one value after evaluation.
Definition: formula.h:451
std::unique_ptr< gum::formula::Scanner > __scanner
The scanner used by the formula.
Definition: formula.h:442

◆ result()

double gum::Formula::result ( ) const

Returns the result of this gum::Formula.

Each call to Formula::result() will reevaluate the formulas result.

Returns
Returns the result of this gum::Formula.

Definition at line 295 of file formula.cpp.

References __output, __parser, __reduceOperatorOrFunction(), and GUM_ERROR.

Referenced by gum::prm::PRMFormAttribute< GUM_SCALAR >::__fillCpf(), gum::operator*(), gum::operator+(), gum::operator-(), gum::operator/(), gum::operator<<(), and gum::to_string().

295  {
296  __parser->Parse();
297 
298  std::stack< FormulaPart > stack;
299  if (__output.empty()) { GUM_ERROR(OperationNotAllowed, "no output found"); }
300 
301  for (auto item : __output) {
302  switch (item.type) {
303  case FormulaPart::token_type::NUMBER: {
304  stack.push(item);
305  break;
306  }
307 
308  case FormulaPart::token_type::OPERATOR:
309  case FormulaPart::token_type::FUNCTION: {
310  __reduceOperatorOrFunction(item, stack);
311  break;
312  }
313 
314  default: {
315  GUM_ERROR(OperationNotAllowed,
316  "expecting numbers, operators or functions");
317  }
318  }
319  }
320 
321  if (stack.size() != 1) {
322  GUM_ERROR(OperationNotAllowed, "too many inputs");
323 
324  } else if (stack.top().type != FormulaPart::token_type::NUMBER) {
325  GUM_ERROR(OperationNotAllowed, "too many inputs");
326  }
327  return stack.top().number;
328  }
void __reduceOperatorOrFunction(FormulaPart item, std::stack< FormulaPart > &stack) const
Evaluate an operator or function and push its result.
Definition: formula_inl.h:342
std::unique_ptr< gum::formula::Parser > __parser
The parser used by the formula.
Definition: formula.h:445
std::vector< FormulaPart > __output
The output stack, will contain one value after evaluation.
Definition: formula.h:451
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ variables() [1/2]

INLINE HashTable< std::string, double > & gum::Formula::variables ( )

Returns the variables used by this gum::Formula.

Returns
Returns the variables used by this gum::Formula.

Definition at line 417 of file formula_inl.h.

Referenced by gum::prm::PRMFormAttribute< GUM_SCALAR >::__fillCpf().

417 { return __variables; }
HashTable< std::string, double > __variables
The variables available in this formula.
Definition: formula.h:457
+ Here is the caller graph for this function:

◆ variables() [2/2]

INLINE const HashTable< std::string, double > & gum::Formula::variables ( ) const

Returns the variables used by this gum::Formula.

Returns
Returns the variables used by this gum::Formula.

Definition at line 420 of file formula_inl.h.

420  {
421  return __variables;
422  }
HashTable< std::string, double > __variables
The variables available in this formula.
Definition: formula.h:457

Friends And Related Function Documentation

◆ gum::formula::Parser

friend class gum::formula::Parser
friend

Definition at line 272 of file formula.h.

Member Data Documentation

◆ __formula

std::string gum::Formula::__formula
private

The formula to evaluate.

Definition at line 439 of file formula.h.

Referenced by operator=().

◆ __last_token

FormulaPart gum::Formula::__last_token
private

The last token added to the formula.

Definition at line 448 of file formula.h.

Referenced by operator=().

◆ __output

std::vector< FormulaPart > gum::Formula::__output
private

The output stack, will contain one value after evaluation.

Definition at line 451 of file formula.h.

Referenced by operator=(), and result().

◆ __parser

std::unique_ptr< gum::formula::Parser > gum::Formula::__parser
private

The parser used by the formula.

Definition at line 445 of file formula.h.

Referenced by Formula(), operator=(), and result().

◆ __scanner

std::unique_ptr< gum::formula::Scanner > gum::Formula::__scanner
private

The scanner used by the formula.

Definition at line 442 of file formula.h.

Referenced by operator=().

◆ __stack

std::stack< FormulaPart > gum::Formula::__stack
private

A stack used during evaluation.

Definition at line 454 of file formula.h.

Referenced by operator=().

◆ __variables

HashTable< std::string, double > gum::Formula::__variables
private

The variables available in this formula.

Definition at line 457 of file formula.h.


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