aGrUM  0.16.3
a C++ library for (probabilistic) graphical models
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 274 of file formula.h.

Constructor & Destructor Documentation

◆ Formula() [1/13]

gum::Formula::Formula ( short  s)

Constructor.

Definition at line 187 of file formula.cpp.

References __initialise().

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

◆ Formula() [2/13]

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

Constructor.

Definition at line 193 of file formula.cpp.

References __initialise().

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

◆ Formula() [3/13]

gum::Formula::Formula ( int  i)

Constructor.

Definition at line 199 of file formula.cpp.

References __initialise().

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

◆ Formula() [4/13]

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

Constructor.

Definition at line 205 of file formula.cpp.

References __initialise().

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

◆ Formula() [5/13]

gum::Formula::Formula ( long  l)

Constructor.

Definition at line 211 of file formula.cpp.

References __initialise().

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

◆ Formula() [6/13]

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

Constructor.

Definition at line 217 of file formula.cpp.

References __initialise().

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

◆ Formula() [7/13]

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

Constructor.

Definition at line 223 of file formula.cpp.

References __initialise().

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

◆ Formula() [8/13]

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

Constructor.

Definition at line 229 of file formula.cpp.

References __initialise().

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

◆ Formula() [9/13]

gum::Formula::Formula ( float  f)

Constructor.

Definition at line 235 of file formula.cpp.

References __initialise().

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

◆ Formula() [10/13]

gum::Formula::Formula ( double  d)

Constructor.

Definition at line 241 of file formula.cpp.

References __initialise().

241  :
242  __formula(std::to_string(d)), __last_token(FormulaPart()) {
243  GUM_CONSTRUCTOR(Formula);
244  __initialise();
245  }
std::string __formula
The formula to evaluate.
Definition: formula.h:442
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:451
std::string to_string(const Formula &f)
Definition: formula_inl.h:499
Formula(short s)
Constructor.
Definition: formula.cpp:187
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
+ 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 247 of file formula.cpp.

References __initialise().

247  :
248  __formula(f), __last_token(FormulaPart()) {
249  GUM_CONSTRUCTOR(Formula);
250 
251  __initialise();
252  }
std::string __formula
The formula to evaluate.
Definition: formula.h:442
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:451
Formula(short s)
Constructor.
Definition: formula.cpp:187
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
+ 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 254 of file formula.cpp.

References __initialise().

254  :
255  __formula(source.__formula), __last_token(source.__last_token),
256  __output(source.__output), __stack(source.__stack) {
257  GUM_CONS_CPY(Formula);
258 
259  __initialise();
260  }
std::string __formula
The formula to evaluate.
Definition: formula.h:442
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:451
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:457
Formula(short s)
Constructor.
Definition: formula.cpp:187
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
std::vector< FormulaPart > __output
The output stack, will contain one value after evaluation.
Definition: formula.h:454
+ 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 262 of file formula.cpp.

References __parser.

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

◆ ~Formula()

gum::Formula::~Formula ( )

Class destructor.

Definition at line 272 of file formula.cpp.

272 { GUM_DESTRUCTOR(Formula); }
Formula(short s)
Constructor.
Definition: formula.cpp:187

Member Function Documentation

◆ __finalize()

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

Finalize the formula and prepare it for evaluation.

Definition at line 349 of file formula_inl.h.

References GUM_ERROR.

349  {
350  while (!__stack.empty()) {
351  if (__stack.top().character == '(') {
352  GUM_ERROR(OperationNotAllowed, "expecting ')'");
353  }
354 
355  __push_output(__stack.top());
356  __stack.pop();
357  }
358  }
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:457
void __push_output(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:379
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

◆ __initialise()

void gum::Formula::__initialise ( )
private

Initialise the formula scanner and parser.

Definition at line 177 of file formula.cpp.

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

177  {
178  auto c_str = (unsigned char*)__formula.c_str();
179  auto scanner = new gum::formula::Scanner(c_str, (int)__formula.size());
180  __scanner = std::unique_ptr< gum::formula::Scanner >(scanner);
181 
182  auto parser = new gum::formula::Parser(scanner);
183  __parser = std::unique_ptr< gum::formula::Parser >(parser);
184  __parser->formula(this);
185  }
std::string __formula
The formula to evaluate.
Definition: formula.h:442
std::unique_ptr< gum::formula::Parser > __parser
The parser used by the formula.
Definition: formula.h:448
friend class gum::formula::Parser
Definition: formula.h:275
std::unique_ptr< gum::formula::Scanner > __scanner
The scanner used by the formula.
Definition: formula.h:445
+ 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 283 of file formula_inl.h.

283  {
284  switch (__last_token.type) {
285  case FormulaPart::token_type::OPERATOR:
286  case FormulaPart::token_type::NIL:
287  case FormulaPart::token_type::ARG_SEP: {
288  return o == '-';
289  }
290 
291  case FormulaPart::token_type::PARENTHESIS: {
292  return (o == '-') && (__last_token.character == '(');
293  }
294 
295  default: {
296  return false;
297  }
298  }
299  }
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:451
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:69
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79

◆ __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 254 of file formula_inl.h.

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

254  {
255  if (__stack.empty()
256  || __stack.top().type != FormulaPart::token_type::OPERATOR) {
257  return false;
258  }
259 
260  if (o.isLeftAssociative() && o.precedence() <= __stack.top().precedence()) {
261  return true;
262  }
263 
264  if (o.isRightAssociative() && o.precedence() < __stack.top().precedence()) {
265  return true;
266  }
267 
268  return false;
269  }
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:457
+ 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 423 of file formula_inl.h.

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

423  {
424  while ((!__stack.empty()) && (__stack.top().character != '(')) {
425  __push_output(__stack.top());
426  __stack.pop();
427  }
428 
429  if (__stack.empty() || __stack.top().character != '(') {
430  GUM_ERROR(OperationNotAllowed, "expecting a '('");
431  }
432 
433  __last_token = FormulaPart(FormulaPart::token_type::ARG_SEP, ',');
434  }
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:451
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:457
void __push_output(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:379
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ 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 391 of file formula_inl.h.

References GUM_ERROR.

391  {
392  if (func == "exp") {
393  FormulaPart t(FormulaPart::token_type::FUNCTION,
394  FormulaPart::token_function::exp);
395  __push_stack(t);
396 
397  } else if (func == "log") {
398  FormulaPart t(FormulaPart::token_type::FUNCTION,
399  FormulaPart::token_function::log);
400  __push_stack(t);
401 
402  } else if (func == "ln") {
403  FormulaPart t(FormulaPart::token_type::FUNCTION,
404  FormulaPart::token_function::ln);
405  __push_stack(t);
406 
407  } else if (func == "pow") {
408  FormulaPart t(FormulaPart::token_type::FUNCTION,
409  FormulaPart::token_function::pow);
410  __push_stack(t);
411 
412  } else if (func == "sqrt") {
413  FormulaPart t(FormulaPart::token_type::FUNCTION,
414  FormulaPart::token_function::sqrt);
415  __push_stack(t);
416 
417  } else {
418  GUM_ERROR(OperationNotAllowed, "unknown function");
419  }
420  }
void __push_stack(FormulaPart t)
Push the gum::FormulaPart in the stack.
Definition: formula_inl.h:385
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

◆ __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 455 of file formula_inl.h.

References GUM_ERROR.

455  {
456  try {
457  __push_function(ident);
458 
459  } catch (OperationNotAllowed&) {
460  try {
461  __push_variable(ident);
462 
463  } catch (OperationNotAllowed&) {
464  GUM_ERROR(OperationNotAllowed, "unknown identifier");
465  }
466  }
467  }
void __push_function(const std::string &func)
Push a function in the formula.
Definition: formula_inl.h:391
void __push_variable(const std::string &var)
Push a variable in the formula.
Definition: formula_inl.h:445
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

◆ __push_leftParenthesis()

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

Push a left parenthesis in the formula.

Definition at line 319 of file formula_inl.h.

319  {
320  FormulaPart t(FormulaPart::token_type::PARENTHESIS, '(');
321  __push_stack(t);
322  }
void __push_stack(FormulaPart t)
Push the gum::FormulaPart in the stack.
Definition: formula_inl.h:385

◆ __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 248 of file formula_inl.h.

248  {
249  FormulaPart t(FormulaPart::token_type::NUMBER, v);
250  __push_output(t);
251  }
void __push_output(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:379

◆ __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 272 of file formula_inl.h.

272  {
273  if (__isUnaryOperator(o)) {
275 
276  } else {
277  FormulaPart t(FormulaPart::token_type::OPERATOR, o);
278  __push_operator(t);
279  }
280  }
bool __isUnaryOperator(char o)
Returns true if o is an unary operator.
Definition: formula_inl.h:283
void __push_operator(char o)
Push an operator in the formula.
Definition: formula_inl.h:272
void __push_unaryOperator(char o)
Push an unary operator.
Definition: formula_inl.h:302

◆ __push_operator() [2/2]

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

Push an operator.

Parameters
tThe operator to push.

Definition at line 309 of file formula_inl.h.

309  {
310  while (__popOperator(t)) {
311  __push_output(__stack.top());
312  __stack.pop();
313  }
314 
315  __push_stack(t);
316  }
bool __popOperator(FormulaPart o)
Pop the operator in the inner formula&#39;s stack.
Definition: formula_inl.h:254
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:457
void __push_stack(FormulaPart t)
Push the gum::FormulaPart in the stack.
Definition: formula_inl.h:385
void __push_output(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:379

◆ __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 379 of file formula_inl.h.

379  {
380  __output.push_back(t);
381  __last_token = t;
382  }
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:451
std::vector< FormulaPart > __output
The output stack, will contain one value after evaluation.
Definition: formula.h:454

◆ __push_rightParenthesis()

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

Push a right parenthesis in the formula.

Definition at line 325 of file formula_inl.h.

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

325  {
326  while ((!__stack.empty()) && (__stack.top().character != '(')) {
327  __push_output(__stack.top());
328  __stack.pop();
329  }
330 
331  if (__stack.empty()) {
332  GUM_ERROR(OperationNotAllowed, "expecting '('");
333 
334  } else if (__stack.top().character != '(') {
335  GUM_ERROR(OperationNotAllowed, "expecting '('");
336  }
337 
338  __stack.pop();
339 
340  if ((!__stack.empty())
341  && __stack.top().type == FormulaPart::token_type::FUNCTION) {
342  __push_output(__stack.top());
343  __stack.pop();
344  }
345  __last_token = FormulaPart(FormulaPart::token_type::PARENTHESIS, ')');
346  }
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:451
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:457
void __push_output(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:379
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ 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 385 of file formula_inl.h.

385  {
386  __stack.push(t);
387  __last_token = t;
388  }
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:451
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:457

◆ __push_unaryOperator()

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

Push an unary operator.

Parameters
oThe unary operator to push.

Definition at line 302 of file formula_inl.h.

302  {
303  // Only unary operator is the negative sign -
304  FormulaPart t(FormulaPart::token_type::OPERATOR, '_');
305  __push_operator(t);
306  }
void __push_operator(char o)
Push an operator in the formula.
Definition: formula_inl.h:272

◆ __push_variable()

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

Push a variable in the formula.

Definition at line 445 of file formula_inl.h.

References GUM_ERROR.

445  {
446  if (__variables.exists(var)) {
448 
449  } else {
450  GUM_ERROR(OperationNotAllowed, "unknonw variable");
451  }
452  }
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:248
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
HashTable< std::string, double > __variables
The variables available in this formula.
Definition: formula.h:460

◆ __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 362 of file formula_inl.h.

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

Referenced by result().

363  {
364  std::vector< FormulaPart > args;
365 
366  if (stack.size() < item.argc()) {
367  GUM_ERROR(OperationNotAllowed, "not enought inputs ");
368  }
369 
370  while (item.argc() > args.size()) {
371  args.push_back(stack.top());
372  stack.pop();
373  }
374 
375  stack.push(item.eval(args));
376  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ 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 242 of file formula_inl.h.

242 { return __formula; }
std::string __formula
The formula to evaluate.
Definition: formula.h:442

◆ formula() [2/2]

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

Returns the formula.

Definition at line 245 of file formula_inl.h.

245 { return __formula; }
std::string __formula
The formula to evaluate.
Definition: formula.h:442

◆ operator double()

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

Allows implicit conversion to doubles.

Definition at line 345 of file formula.h.

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

◆ 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 274 of file formula.cpp.

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

274  {
275  if (this == &source) { return *this; }
276 
277  __formula = source.__formula;
278  __last_token = source.__last_token;
279  __output = source.__output;
280  __stack = source.__stack;
281 
282  __initialise();
283 
284  return *this;
285  }
std::string __formula
The formula to evaluate.
Definition: formula.h:442
FormulaPart __last_token
The last token added to the formula.
Definition: formula.h:451
std::stack< FormulaPart > __stack
A stack used during evaluation.
Definition: formula.h:457
void __initialise()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
std::vector< FormulaPart > __output
The output stack, will contain one value after evaluation.
Definition: formula.h:454
+ 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 287 of file formula.cpp.

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

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

◆ 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 301 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().

301  {
302  __parser->Parse();
303 
304  std::stack< FormulaPart > stack;
305  if (__output.empty()) { GUM_ERROR(OperationNotAllowed, "no output found"); }
306 
307  for (auto item : __output) {
308  switch (item.type) {
309  case FormulaPart::token_type::NUMBER: {
310  stack.push(item);
311  break;
312  }
313 
314  case FormulaPart::token_type::OPERATOR:
315  case FormulaPart::token_type::FUNCTION: {
316  __reduceOperatorOrFunction(item, stack);
317  break;
318  }
319 
320  default: {
321  GUM_ERROR(OperationNotAllowed,
322  "expecting numbers, operators or functions");
323  }
324  }
325  }
326 
327  if (stack.size() != 1) {
328  GUM_ERROR(OperationNotAllowed, "too many inputs");
329 
330  } else if (stack.top().type != FormulaPart::token_type::NUMBER) {
331  GUM_ERROR(OperationNotAllowed, "too many inputs");
332  }
333  return stack.top().number;
334  }
void __reduceOperatorOrFunction(FormulaPart item, std::stack< FormulaPart > &stack) const
Evaluate an operator or function and push its result.
Definition: formula_inl.h:362
std::unique_ptr< gum::formula::Parser > __parser
The parser used by the formula.
Definition: formula.h:448
std::vector< FormulaPart > __output
The output stack, will contain one value after evaluation.
Definition: formula.h:454
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ 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 437 of file formula_inl.h.

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

437 { return __variables; }
HashTable< std::string, double > __variables
The variables available in this formula.
Definition: formula.h:460
+ 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 440 of file formula_inl.h.

440  {
441  return __variables;
442  }
HashTable< std::string, double > __variables
The variables available in this formula.
Definition: formula.h:460

Friends And Related Function Documentation

◆ gum::formula::Parser

friend class gum::formula::Parser
friend

Definition at line 275 of file formula.h.

Member Data Documentation

◆ __formula

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

The formula to evaluate.

Definition at line 442 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 451 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 454 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 448 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 445 of file formula.h.

Referenced by operator=().

◆ __stack

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

A stack used during evaluation.

Definition at line 457 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 460 of file formula.h.


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