aGrUM  0.20.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 289 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 gum::Set< Key, Alloc >::emplace().

187  : _formula_(std::to_string(s)), _last_token_(FormulaPart()) {
188  GUM_CONSTRUCTOR(Formula);
189  _initialise_();
190  }
void _initialise_()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ Here is the call graph for this function:

◆ Formula() [2/13]

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

Constructor.

Definition at line 192 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

192  : _formula_(std::to_string(us)), _last_token_(FormulaPart()) {
193  GUM_CONSTRUCTOR(Formula);
194  _initialise_();
195  }
void _initialise_()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ Here is the call graph for this function:

◆ Formula() [3/13]

gum::Formula::Formula ( int  i)

Constructor.

Definition at line 197 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

197  : _formula_(std::to_string(i)), _last_token_(FormulaPart()) {
198  GUM_CONSTRUCTOR(Formula);
199  _initialise_();
200  }
void _initialise_()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ Here is the call graph for this function:

◆ Formula() [4/13]

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

Constructor.

Definition at line 202 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

202  : _formula_(std::to_string(ui)), _last_token_(FormulaPart()) {
203  GUM_CONSTRUCTOR(Formula);
204  _initialise_();
205  }
void _initialise_()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ Here is the call graph for this function:

◆ Formula() [5/13]

gum::Formula::Formula ( long  l)

Constructor.

Definition at line 207 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

207  : _formula_(std::to_string(l)), _last_token_(FormulaPart()) {
208  GUM_CONSTRUCTOR(Formula);
209  _initialise_();
210  }
void _initialise_()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ Here is the call graph for this function:

◆ Formula() [6/13]

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

Constructor.

Definition at line 212 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

212  : _formula_(std::to_string(ul)), _last_token_(FormulaPart()) {
213  GUM_CONSTRUCTOR(Formula);
214  _initialise_();
215  }
void _initialise_()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ 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 gum::Set< Key, Alloc >::emplace().

217  : _formula_(std::to_string(l)), _last_token_(FormulaPart()) {
218  GUM_CONSTRUCTOR(Formula);
219  _initialise_();
220  }
void _initialise_()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ Here is the call graph for this function:

◆ Formula() [8/13]

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

Constructor.

Definition at line 222 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

222  :
223  _formula_(std::to_string(ul)), _last_token_(FormulaPart()) {
224  GUM_CONSTRUCTOR(Formula);
225  _initialise_();
226  }
void _initialise_()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ Here is the call graph for this function:

◆ Formula() [9/13]

gum::Formula::Formula ( float  f)

Constructor.

Definition at line 228 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

228  : _formula_(std::to_string(f)), _last_token_(FormulaPart()) {
229  GUM_CONSTRUCTOR(Formula);
230  _initialise_();
231  }
void _initialise_()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ Here is the call graph for this function:

◆ Formula() [10/13]

gum::Formula::Formula ( double  d)

Constructor.

Definition at line 233 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

233  : _formula_(std::to_string(d)), _last_token_(FormulaPart()) {
234  GUM_CONSTRUCTOR(Formula);
235  _initialise_();
236  }
void _initialise_()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ 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 238 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

238  : _formula_(f), _last_token_(FormulaPart()) {
239  GUM_CONSTRUCTOR(Formula);
240 
241  _initialise_();
242  }
void _initialise_()
Initialise the formula scanner and parser.
Definition: formula.cpp:177
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ 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 244 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

244  :
245  _formula_(source._formula_), _last_token_(source._last_token_), _output_(source._output_),
246  _stack_(source._stack_) {
247  GUM_CONS_CPY(Formula);
248 
249  _initialise_();
250  }
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:469
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::stack< FormulaPart > _stack_
A stack used during evaluation.
Definition: formula.h:472
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ 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 252 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

252  :
253  _formula_(std::move(source._formula_)), _scanner_(std::move(source._scanner_)),
254  _parser_(std::move(source._parser_)), _last_token_(std::move(source._last_token_)),
255  _output_(std::move(source._output_)), _stack_(std::move(source._stack_)) {
256  GUM_CONS_CPY(Formula);
257 
258  _parser_->formula(this);
259  }
std::unique_ptr< gum::formula::Scanner > _scanner_
The scanner used by the formula.
Definition: formula.h:460
std::unique_ptr< gum::formula::Parser > _parser_
The parser used by the formula.
Definition: formula.h:463
std::vector< FormulaPart > _output_
The output stack, will contain one value after evaluation.
Definition: formula.h:469
Formula(short s)
Constructor.
Definition: formula.cpp:187
std::stack< FormulaPart > _stack_
A stack used during evaluation.
Definition: formula.h:472
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ Here is the call graph for this function:

◆ ~Formula()

gum::Formula::~Formula ( )

Class destructor.

Definition at line 261 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

261  {
262  GUM_DESTRUCTOR(Formula);
263  ;
264  }
Formula(short s)
Constructor.
Definition: formula.cpp:187
+ Here is the call graph for this function:

Member Function Documentation

◆ _finalize_()

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

Finalize the formula and prepare it for evaluation.

Definition at line 340 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

340  {
341  while (!_stack_.empty()) {
342  if (_stack_.top().character == '(') { GUM_ERROR(OperationNotAllowed, "expecting ')'") }
343 
344  _push_output_(_stack_.top());
345  _stack_.pop();
346  }
347  }
std::stack< FormulaPart > _stack_
A stack used during evaluation.
Definition: formula.h:472
void _push_output_(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:365
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ _initialise_()

void gum::Formula::_initialise_ ( )
private

Initialise the formula scanner and parser.

Definition at line 177 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

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:460
std::unique_ptr< gum::formula::Parser > _parser_
The parser used by the formula.
Definition: formula.h:463
friend class gum::formula::Parser
Definition: formula.h:290
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
+ Here is the call 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 275 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

275  {
276  switch (_last_token_.type) {
277  case FormulaPart::token_type::OPERATOR:
278  case FormulaPart::token_type::NIL:
279  case FormulaPart::token_type::ARG_SEP: {
280  return o == '-';
281  }
282 
283  case FormulaPart::token_type::PARENTHESIS: {
284  return (o == '-') && (_last_token_.character == '(');
285  }
286 
287  default: {
288  return false;
289  }
290  }
291  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:84
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:94
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ Here is the call graph for this function:

◆ _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 251 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

251  {
252  if (_stack_.empty() || _stack_.top().type != FormulaPart::token_type::OPERATOR) {
253  return false;
254  }
255 
256  if (o.isLeftAssociative() && o.precedence() <= _stack_.top().precedence()) { return true; }
257 
258  if (o.isRightAssociative() && o.precedence() < _stack_.top().precedence()) { return true; }
259 
260  return false;
261  }
std::stack< FormulaPart > _stack_
A stack used during evaluation.
Definition: formula.h:472
+ 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 404 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

404  {
405  while ((!_stack_.empty()) && (_stack_.top().character != '(')) {
406  _push_output_(_stack_.top());
407  _stack_.pop();
408  }
409 
410  if (_stack_.empty() || _stack_.top().character != '(') {
411  GUM_ERROR(OperationNotAllowed, "expecting a '('")
412  }
413 
414  _last_token_ = FormulaPart(FormulaPart::token_type::ARG_SEP, ',');
415  }
std::stack< FormulaPart > _stack_
A stack used during evaluation.
Definition: formula.h:472
void _push_output_(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:365
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ 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 377 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

377  {
378  if (func == "exp") {
379  FormulaPart t(FormulaPart::token_type::FUNCTION, FormulaPart::token_function::exp);
380  _push_stack_(t);
381 
382  } else if (func == "log") {
383  FormulaPart t(FormulaPart::token_type::FUNCTION, FormulaPart::token_function::log);
384  _push_stack_(t);
385 
386  } else if (func == "ln") {
387  FormulaPart t(FormulaPart::token_type::FUNCTION, FormulaPart::token_function::ln);
388  _push_stack_(t);
389 
390  } else if (func == "pow") {
391  FormulaPart t(FormulaPart::token_type::FUNCTION, FormulaPart::token_function::pow);
392  _push_stack_(t);
393 
394  } else if (func == "sqrt") {
395  FormulaPart t(FormulaPart::token_type::FUNCTION, FormulaPart::token_function::sqrt);
396  _push_stack_(t);
397 
398  } else {
399  GUM_ERROR(OperationNotAllowed, "unknown function")
400  }
401  }
void _push_stack_(FormulaPart t)
Push the gum::FormulaPart in the stack.
Definition: formula_inl.h:371
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ _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 434 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

434  {
435  try {
436  _push_function_(ident);
437 
438  } catch (OperationNotAllowed&) {
439  try {
440  _push_variable_(ident);
441 
442  } catch (OperationNotAllowed&) { GUM_ERROR(OperationNotAllowed, "unknown identifier") }
443  }
444  }
void _push_function_(const std::string &func)
Push a function in the formula.
Definition: formula_inl.h:377
void _push_variable_(const std::string &var)
Push a variable in the formula.
Definition: formula_inl.h:424
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ _push_leftParenthesis_()

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

Push a left parenthesis in the formula.

Definition at line 311 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

311  {
312  FormulaPart t(FormulaPart::token_type::PARENTHESIS, '(');
313  _push_stack_(t);
314  }
void _push_stack_(FormulaPart t)
Push the gum::FormulaPart in the stack.
Definition: formula_inl.h:371
+ Here is the call graph for this function:

◆ _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 245 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

245  {
246  FormulaPart t(FormulaPart::token_type::NUMBER, v);
247  _push_output_(t);
248  }
void _push_output_(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:365
+ Here is the call graph for this function:

◆ _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 264 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

264  {
265  if (_isUnaryOperator_(o)) {
267 
268  } else {
269  FormulaPart t(FormulaPart::token_type::OPERATOR, o);
270  _push_operator_(t);
271  }
272  }
void _push_operator_(char o)
Push an operator in the formula.
Definition: formula_inl.h:264
bool _isUnaryOperator_(char o)
Returns true if o is an unary operator.
Definition: formula_inl.h:275
void _push_unaryOperator_(char o)
Push an unary operator.
Definition: formula_inl.h:294
+ Here is the call graph for this function:

◆ _push_operator_() [2/2]

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

Push an operator.

Parameters
tThe operator to push.

Definition at line 301 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

301  {
302  while (_popOperator_(t)) {
303  _push_output_(_stack_.top());
304  _stack_.pop();
305  }
306 
307  _push_stack_(t);
308  }
void _push_stack_(FormulaPart t)
Push the gum::FormulaPart in the stack.
Definition: formula_inl.h:371
bool _popOperator_(FormulaPart o)
Pop the operator in the inner formula&#39;s stack.
Definition: formula_inl.h:251
std::stack< FormulaPart > _stack_
A stack used during evaluation.
Definition: formula.h:472
void _push_output_(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:365
+ Here is the call graph for this function:

◆ _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 365 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

365  {
366  _output_.push_back(t);
367  _last_token_ = t;
368  }
std::vector< FormulaPart > _output_
The output stack, will contain one value after evaluation.
Definition: formula.h:469
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ Here is the call graph for this function:

◆ _push_rightParenthesis_()

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

Push a right parenthesis in the formula.

Definition at line 317 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

317  {
318  while ((!_stack_.empty()) && (_stack_.top().character != '(')) {
319  _push_output_(_stack_.top());
320  _stack_.pop();
321  }
322 
323  if (_stack_.empty()) {
324  GUM_ERROR(OperationNotAllowed, "expecting '('")
325 
326  } else if (_stack_.top().character != '(') {
327  GUM_ERROR(OperationNotAllowed, "expecting '('")
328  }
329 
330  _stack_.pop();
331 
332  if ((!_stack_.empty()) && _stack_.top().type == FormulaPart::token_type::FUNCTION) {
333  _push_output_(_stack_.top());
334  _stack_.pop();
335  }
336  _last_token_ = FormulaPart(FormulaPart::token_type::PARENTHESIS, ')');
337  }
std::stack< FormulaPart > _stack_
A stack used during evaluation.
Definition: formula.h:472
void _push_output_(FormulaPart t)
Push the gum::FormulaPart in the output vector.
Definition: formula_inl.h:365
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ 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 371 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

371  {
372  _stack_.push(t);
373  _last_token_ = t;
374  }
std::stack< FormulaPart > _stack_
A stack used during evaluation.
Definition: formula.h:472
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ Here is the call graph for this function:

◆ _push_unaryOperator_()

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

Push an unary operator.

Parameters
oThe unary operator to push.

Definition at line 294 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

294  {
295  // Only unary operator is the negative sign -
296  FormulaPart t(FormulaPart::token_type::OPERATOR, '_');
297  _push_operator_(t);
298  }
void _push_operator_(char o)
Push an operator in the formula.
Definition: formula_inl.h:264
+ Here is the call graph for this function:

◆ _push_variable_()

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

Push a variable in the formula.

Definition at line 424 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

424  {
425  if (_variables_.exists(var)) {
427 
428  } else {
429  GUM_ERROR(OperationNotAllowed, "unknonw variable")
430  }
431  }
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:245
HashTable< std::string, double > _variables_
The variables available in this formula.
Definition: formula.h:475
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ _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 350 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

351  {
352  std::vector< FormulaPart > args;
353 
354  if (stack.size() < item.argc()) { GUM_ERROR(OperationNotAllowed, "not enought inputs ") }
355 
356  while (item.argc() > args.size()) {
357  args.push_back(stack.top());
358  stack.pop();
359  }
360 
361  stack.push(item.eval(args));
362  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ formula() [1/2]

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

Returns the formula.

Definition at line 239 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

239 { return _formula_; }
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
+ Here is the call graph for this function:

◆ formula() [2/2]

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

Returns the formula.

Definition at line 242 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

242 { return _formula_; }
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
+ Here is the call graph for this function:

◆ operator double()

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

Allows implicit conversion to doubles.

Definition at line 360 of file formula.h.

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

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

References gum::Set< Key, Alloc >::emplace().

266  {
267  if (this == &source) { return *this; }
268 
269  _formula_ = source._formula_;
270  _last_token_ = source._last_token_;
271  _output_ = source._output_;
272  _stack_ = source._stack_;
273 
274  _initialise_();
275 
276  return *this;
277  }
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:469
std::stack< FormulaPart > _stack_
A stack used during evaluation.
Definition: formula.h:472
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ 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 279 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

279  {
280  if (this == &source) { return *this; }
281 
282  _formula_ = std::move(source._formula_);
283  _scanner_ = std::move(source._scanner_);
284  _parser_ = std::move(source._parser_);
285  _parser_->formula(this);
286  _last_token_ = std::move(source._last_token_);
287  _output_ = std::move(source._output_);
288  _stack_ = std::move(source._stack_);
289 
290  return *this;
291  }
std::unique_ptr< gum::formula::Scanner > _scanner_
The scanner used by the formula.
Definition: formula.h:460
std::unique_ptr< gum::formula::Parser > _parser_
The parser used by the formula.
Definition: formula.h:463
std::vector< FormulaPart > _output_
The output stack, will contain one value after evaluation.
Definition: formula.h:469
std::stack< FormulaPart > _stack_
A stack used during evaluation.
Definition: formula.h:472
std::string _formula_
The formula to evaluate.
Definition: formula.h:457
FormulaPart _last_token_
The last token added to the formula.
Definition: formula.h:466
+ Here is the call 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 293 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

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

References gum::Set< Key, Alloc >::emplace().

418 { return _variables_; }
HashTable< std::string, double > _variables_
The variables available in this formula.
Definition: formula.h:475
+ Here is the call 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 421 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

421 { return _variables_; }
HashTable< std::string, double > _variables_
The variables available in this formula.
Definition: formula.h:475
+ Here is the call graph for this function:

Friends And Related Function Documentation

◆ gum::formula::Parser

friend class gum::formula::Parser
friend

Definition at line 290 of file formula.h.

Member Data Documentation

◆ _formula_

std::string gum::Formula::_formula_
private

The formula to evaluate.

Definition at line 457 of file formula.h.

◆ _last_token_

FormulaPart gum::Formula::_last_token_
private

The last token added to the formula.

Definition at line 466 of file formula.h.

◆ _output_

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

The output stack, will contain one value after evaluation.

Definition at line 469 of file formula.h.

◆ _parser_

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

The parser used by the formula.

Definition at line 463 of file formula.h.

◆ _scanner_

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

The scanner used by the formula.

Definition at line 460 of file formula.h.

◆ _stack_

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

A stack used during evaluation.

Definition at line 472 of file formula.h.

◆ _variables_

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

The variables available in this formula.

Definition at line 475 of file formula.h.


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