aGrUM  0.18.1
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  }
void initialise__()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
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
std::string formula__
The formula to evaluate.
Definition: formula.h:442
+ 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  }
void initialise__()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
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
std::string formula__
The formula to evaluate.
Definition: formula.h:442
+ 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  }
void initialise__()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
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
std::string formula__
The formula to evaluate.
Definition: formula.h:442
+ 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  }
void initialise__()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
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
std::string formula__
The formula to evaluate.
Definition: formula.h:442
+ 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  }
void initialise__()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
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
std::string formula__
The formula to evaluate.
Definition: formula.h:442
+ 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  }
void initialise__()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
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
std::string formula__
The formula to evaluate.
Definition: formula.h:442
+ 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  }
void initialise__()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
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
std::string formula__
The formula to evaluate.
Definition: formula.h:442
+ 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  }
void initialise__()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
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
std::string formula__
The formula to evaluate.
Definition: formula.h:442
+ 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  }
void initialise__()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
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
std::string formula__
The formula to evaluate.
Definition: formula.h:442
+ 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  }
void initialise__()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
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
std::string formula__
The formula to evaluate.
Definition: formula.h:442
+ 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  }
void initialise__()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
FormulaPart last_token__
The last token added to the formula.
Definition: formula.h:451
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::string formula__
The formula to evaluate.
Definition: formula.h:442
+ 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  }
void initialise__()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
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
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::stack< FormulaPart > stack__
A stack used during evaluation.
Definition: formula.h:457
std::string formula__
The formula to evaluate.
Definition: formula.h:442
+ 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::unique_ptr< gum::formula::Scanner > scanner__
The scanner used by the formula.
Definition: formula.h:445
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
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::unique_ptr< gum::formula::Parser > parser__
The parser used by the formula.
Definition: formula.h:448
std::stack< FormulaPart > stack__
A stack used during evaluation.
Definition: formula.h:457
std::string formula__
The formula to evaluate.
Definition: formula.h:442

◆ ~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  }
void push_output__(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:379
std::stack< FormulaPart > stack__
A stack used during evaluation.
Definition: formula.h:457
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

◆ 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

◆ 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::unique_ptr< gum::formula::Scanner > scanner__
The scanner used by the formula.
Definition: formula.h:445
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::string formula__
The formula to evaluate.
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 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

◆ 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  }
void initialise__()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
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
std::stack< FormulaPart > stack__
A stack used during evaluation.
Definition: formula.h:457
std::string formula__
The formula to evaluate.
Definition: formula.h:442
+ 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::unique_ptr< gum::formula::Scanner > scanner__
The scanner used by the formula.
Definition: formula.h:445
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
std::unique_ptr< gum::formula::Parser > parser__
The parser used by the formula.
Definition: formula.h:448
std::stack< FormulaPart > stack__
A stack used during evaluation.
Definition: formula.h:457
std::string formula__
The formula to evaluate.
Definition: formula.h:442

◆ 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  }
void push_output__(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:379
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
#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_variable__(const std::string &var)
Push a variable in the formula.
Definition: formula_inl.h:445
void push_function__(const std::string &func)
Push a function in the formula.
Definition: formula_inl.h:391
#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  }
void push_unaryOperator__(char o)
Push an unary operator.
Definition: formula_inl.h:302
void push_operator__(char o)
Push an operator in the formula.
Definition: formula_inl.h:272
bool isUnaryOperator__(char o)
Returns true if o is an unary operator.
Definition: formula_inl.h:283

◆ 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  }
void push_output__(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:379
void push_stack__(FormulaPart t)
Push the gum::FormulaPart in the stack.
Definition: formula_inl.h:385
std::stack< FormulaPart > stack__
A stack used during evaluation.
Definition: formula.h:457
bool popOperator__(FormulaPart o)
Pop the operator in the inner formula&#39;s stack.
Definition: formula_inl.h:254

◆ 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  }
void push_output__(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:379
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
#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.
HashTable< std::string, double > variables__
The variables available in this formula.
Definition: formula.h:460
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

◆ 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:

◆ 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 GUM_ERROR, output__, parser__, and reduceOperatorOrFunction__().

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::vector< FormulaPart > output__
The output stack, will contain one value after evaluation.
Definition: formula.h:454
std::unique_ptr< gum::formula::Parser > parser__
The parser used by the formula.
Definition: formula.h:448
#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: