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

Private Member Functions

void _initialise_ ()
 Initialise the formula scanner and parser.
bool _popOperator_ (FormulaPart o)
 Pop the operator in the inner formula's stack.
void _reduceOperatorOrFunction_ (FormulaPart item, std::stack< FormulaPart > &stack) const
 Evaluate an operator or function and push its result.
void _push_unaryOperator_ (char o)
 Push an unary operator.
void _push_operator_ (FormulaPart t)
 Push an operator.
bool _isUnaryOperator_ (char o)
 Returns true if o is an unary operator.
void _push_output_ (FormulaPart t)
 Push the gum::FormulaPart in the output vector.
void _push_stack_ (FormulaPart t)
 Push the gum::FormulaPart in the stack.
Private accessors & modifiers used by the Formula Parser
void _push_number_ (const double &v)
 Push a number in the formula.
void _push_operator_ (char o)
 Push an operator in the formula.
void _push_leftParenthesis_ ()
 Push a left parenthesis in the formula.
void _push_rightParenthesis_ ()
 Push a right parenthesis in the formula.
void _push_function_ (const std::string &func)
 Push a function in the formula.
void _push_variable_ (const std::string &var)
 Push a variable in the formula.
void _push_identifier_ (const std::string &ident)
 Use this if you don't know if ident is a function or a variable.
void _push_comma_ ()
 Push a comma in the formula.
void _finalize_ ()
 Finalize the formula and prepare it for evaluation.

Private Attributes

std::string _formula_
 The formula to evaluate.
std::unique_ptr< gum::formula::Scanner > _scanner_
 The scanner used by the formula.
std::unique_ptr< gum::formula::Parser > _parser_
 The parser used by the formula.
FormulaPart _last_token_
 The last token added to the formula.
std::vector< FormulaPart_output_
 The output stack, will contain one value after evaluation.
std::stack< FormulaPart_stack_
 A stack used during evaluation.
HashTable< std::string, double_variables_
 The variables available in this formula.

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 293 of file formula.h.

Constructor & Destructor Documentation

◆ Formula() [1/13]

gum::Formula::Formula ( short s)

Constructor.

Definition at line 207 of file formula.cpp.

207 : _formula_(std::to_string(s)), _last_token_(FormulaPart()) {
208 GUM_CONSTRUCTOR(Formula);
209 _initialise_();
210 }
void _initialise_()
Initialise the formula scanner and parser.
Definition formula.cpp:197
std::string _formula_
The formula to evaluate.
Definition formula.h:461
Formula(short s)
Constructor.
Definition formula.cpp:207
FormulaPart _last_token_
The last token added to the formula.
Definition formula.h:470

References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().

Referenced by Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), Formula(), ~Formula(), gum::formula::Parser, operator=(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Formula() [2/13]

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

Constructor.

Definition at line 212 of file formula.cpp.

212 : _formula_(std::to_string(us)), _last_token_(FormulaPart()) {
213 GUM_CONSTRUCTOR(Formula);
214 _initialise_();
215 }

References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().

Here is the call graph for this function:

◆ Formula() [3/13]

gum::Formula::Formula ( int i)

Constructor.

Definition at line 217 of file formula.cpp.

217 : _formula_(std::to_string(i)), _last_token_(FormulaPart()) {
218 GUM_CONSTRUCTOR(Formula);
219 _initialise_();
220 }

References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().

Here is the call graph for this function:

◆ Formula() [4/13]

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

Constructor.

Definition at line 222 of file formula.cpp.

222 : _formula_(std::to_string(ui)), _last_token_(FormulaPart()) {
223 GUM_CONSTRUCTOR(Formula);
224 _initialise_();
225 }

References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().

Here is the call graph for this function:

◆ Formula() [5/13]

gum::Formula::Formula ( long l)

Constructor.

Definition at line 227 of file formula.cpp.

227 : _formula_(std::to_string(l)), _last_token_(FormulaPart()) {
228 GUM_CONSTRUCTOR(Formula);
229 _initialise_();
230 }

References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().

Here is the call graph for this function:

◆ Formula() [6/13]

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

Constructor.

Definition at line 232 of file formula.cpp.

232 : _formula_(std::to_string(ul)), _last_token_(FormulaPart()) {
233 GUM_CONSTRUCTOR(Formula);
234 _initialise_();
235 }

References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().

Here is the call graph for this function:

◆ Formula() [7/13]

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

Constructor.

Definition at line 237 of file formula.cpp.

237 : _formula_(std::to_string(l)), _last_token_(FormulaPart()) {
238 GUM_CONSTRUCTOR(Formula);
239 _initialise_();
240 }

References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().

Here is the call graph for this function:

◆ Formula() [8/13]

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

Constructor.

Definition at line 242 of file formula.cpp.

242 :
243 _formula_(std::to_string(ul)), _last_token_(FormulaPart()) {
244 GUM_CONSTRUCTOR(Formula);
245 _initialise_();
246 }

References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().

Here is the call graph for this function:

◆ Formula() [9/13]

gum::Formula::Formula ( float f)

Constructor.

Definition at line 248 of file formula.cpp.

248 : _formula_(std::to_string(f)), _last_token_(FormulaPart()) {
249 GUM_CONSTRUCTOR(Formula);
250 _initialise_();
251 }

References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().

Here is the call graph for this function:

◆ Formula() [10/13]

gum::Formula::Formula ( double d)

Constructor.

Definition at line 253 of file formula.cpp.

253 : _formula_(std::to_string(d)), _last_token_(FormulaPart()) {
254 GUM_CONSTRUCTOR(Formula);
255 _initialise_();
256 }

References Formula(), _formula_, _initialise_(), _last_token_, and gum::to_string().

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

258 : _formula_(f), _last_token_(FormulaPart()) {
259 GUM_CONSTRUCTOR(Formula);
260
261 _initialise_();
262 }

References Formula(), _formula_, _initialise_(), and _last_token_.

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

264 :
265 _formula_(source._formula_), _last_token_(source._last_token_), _output_(source._output_),
266 _stack_(source._stack_) {
267 GUM_CONS_CPY(Formula);
268
269 _initialise_();
270 }
std::vector< FormulaPart > _output_
The output stack, will contain one value after evaluation.
Definition formula.h:473
std::stack< FormulaPart > _stack_
A stack used during evaluation.
Definition formula.h:476

References Formula(), _formula_, _initialise_(), _last_token_, _output_, and _stack_.

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

272 :
273 _formula_(std::move(source._formula_)), _scanner_(std::move(source._scanner_)),
274 _parser_(std::move(source._parser_)), _last_token_(std::move(source._last_token_)),
275 _output_(std::move(source._output_)), _stack_(std::move(source._stack_)) {
276 GUM_CONS_CPY(Formula);
277
278 _parser_->formula(this);
279 }
std::unique_ptr< gum::formula::Parser > _parser_
The parser used by the formula.
Definition formula.h:467
std::unique_ptr< gum::formula::Scanner > _scanner_
The scanner used by the formula.
Definition formula.h:464

References Formula(), _formula_, _last_token_, _output_, _parser_, _scanner_, and _stack_.

Here is the call graph for this function:

◆ ~Formula()

gum::Formula::~Formula ( )

Class destructor.

Definition at line 281 of file formula.cpp.

281 {
282 GUM_DESTRUCTOR(Formula);
283 ;
284 }

References Formula().

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 361 of file formula_inl.h.

361 {
362 while (!_stack_.empty()) {
363 if (_stack_.top().character == '(') { GUM_ERROR(OperationNotAllowed, "expecting ')'") }
364
365 _push_output_(_stack_.top());
366 _stack_.pop();
367 }
368 }
void _push_output_(FormulaPart t)
Push the gum::FormulaPart in the output vector.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References _push_output_(), _stack_, and GUM_ERROR.

Here is the call graph for this function:

◆ _initialise_()

void gum::Formula::_initialise_ ( )
private

Initialise the formula scanner and parser.

Definition at line 197 of file formula.cpp.

197 {
198 auto c_str = (unsigned char*)_formula_.c_str();
199 auto scanner = new gum::formula::Scanner(c_str, (int)_formula_.size());
200 _scanner_ = std::unique_ptr< gum::formula::Scanner >(scanner);
201
202 auto parser = new gum::formula::Parser(scanner);
203 _parser_ = std::unique_ptr< gum::formula::Parser >(parser);
204 _parser_->formula(this);
205 }
friend class gum::formula::Parser
Definition formula.h:294

References _formula_, _parser_, _scanner_, and gum::formula::Parser.

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

Here is the call graph for this function:
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 296 of file formula_inl.h.

296 {
297 switch (_last_token_.type) {
301 return o == '-';
302 }
303
305 return (o == '-') && (_last_token_.character == '(');
306 }
307
308 default : {
309 return false;
310 }
311 }
312 }

References _last_token_, gum::FormulaPart::ARG_SEP, gum::FormulaPart::NIL, gum::FormulaPart::OPERATOR, and gum::FormulaPart::PARENTHESIS.

Referenced by _push_operator_().

Here is the caller 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 272 of file formula_inl.h.

272 {
273 if (_stack_.empty() || _stack_.top().type != FormulaPart::token_type::OPERATOR) {
274 return false;
275 }
276
277 if (o.isLeftAssociative() && o.precedence() <= _stack_.top().precedence()) { return true; }
278
279 if (o.isRightAssociative() && o.precedence() < _stack_.top().precedence()) { return true; }
280
281 return false;
282 }

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

Referenced by _push_operator_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _push_comma_()

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

Push a comma in the formula.

Definition at line 425 of file formula_inl.h.

425 {
426 while ((!_stack_.empty()) && (_stack_.top().character != '(')) {
427 _push_output_(_stack_.top());
428 _stack_.pop();
429 }
430
431 if (_stack_.empty() || _stack_.top().character != '(') {
432 GUM_ERROR(OperationNotAllowed, "expecting a '('")
433 }
434
436 }

References _last_token_, _push_output_(), _stack_, gum::FormulaPart::ARG_SEP, and GUM_ERROR.

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 398 of file formula_inl.h.

398 {
399 if (func == "exp") {
401 _push_stack_(t);
402
403 } else if (func == "log") {
405 _push_stack_(t);
406
407 } else if (func == "ln") {
409 _push_stack_(t);
410
411 } else if (func == "pow") {
413 _push_stack_(t);
414
415 } else if (func == "sqrt") {
417 _push_stack_(t);
418
419 } else {
420 GUM_ERROR(OperationNotAllowed, "unknown function")
421 }
422 }
void _push_stack_(FormulaPart t)
Push the gum::FormulaPart in the stack.

References _push_stack_(), gum::FormulaPart::exp, gum::FormulaPart::FUNCTION, GUM_ERROR, gum::FormulaPart::ln, gum::FormulaPart::log, gum::FormulaPart::pow, and gum::FormulaPart::sqrt.

Referenced by _push_identifier_().

Here is the call graph for this function:
Here is the caller 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 455 of file formula_inl.h.

455 {
456 try {
457 _push_function_(ident);
458
459 } catch (OperationNotAllowed const&) {
460 try {
461 _push_variable_(ident);
462
463 } catch (OperationNotAllowed const&) { GUM_ERROR(OperationNotAllowed, "unknown identifier") }
464 }
465 }
void _push_variable_(const std::string &var)
Push a variable in the formula.
void _push_function_(const std::string &func)
Push a function in the formula.

References _push_function_(), _push_variable_(), and GUM_ERROR.

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 332 of file formula_inl.h.

332 {
333 FormulaPart t(FormulaPart::token_type::PARENTHESIS, '(');
334 _push_stack_(t);
335 }

References _push_stack_(), and gum::FormulaPart::PARENTHESIS.

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 266 of file formula_inl.h.

266 {
267 FormulaPart t(FormulaPart::token_type::NUMBER, v);
268 _push_output_(t);
269 }

References _push_output_(), and gum::FormulaPart::NUMBER.

Referenced by _push_variable_().

Here is the call graph for this function:
Here is the caller 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 285 of file formula_inl.h.

285 {
286 if (_isUnaryOperator_(o)) {
288
289 } else {
290 FormulaPart t(FormulaPart::token_type::OPERATOR, o);
292 }
293 }
void _push_unaryOperator_(char o)
Push an unary operator.
bool _isUnaryOperator_(char o)
Returns true if o is an unary operator.
void _push_operator_(char o)
Push an operator in the formula.

References _isUnaryOperator_(), _push_operator_(), _push_unaryOperator_(), and gum::FormulaPart::OPERATOR.

Referenced by _push_operator_(), and _push_unaryOperator_().

Here is the call graph for this function:
Here is the caller 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 322 of file formula_inl.h.

322 {
323 while (_popOperator_(t)) {
324 _push_output_(_stack_.top());
325 _stack_.pop();
326 }
327
328 _push_stack_(t);
329 }
bool _popOperator_(FormulaPart o)
Pop the operator in the inner formula's stack.

References _popOperator_(), _push_output_(), _push_stack_(), and _stack_.

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 386 of file formula_inl.h.

386 {
387 _output_.push_back(t);
388 _last_token_ = t;
389 }

References _last_token_, and _output_.

Referenced by _finalize_(), _push_comma_(), _push_number_(), _push_operator_(), and _push_rightParenthesis_().

Here is the caller graph for this function:

◆ _push_rightParenthesis_()

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

Push a right parenthesis in the formula.

Definition at line 338 of file formula_inl.h.

338 {
339 while ((!_stack_.empty()) && (_stack_.top().character != '(')) {
340 _push_output_(_stack_.top());
341 _stack_.pop();
342 }
343
344 if (_stack_.empty()) {
345 GUM_ERROR(OperationNotAllowed, "expecting '('")
346
347 } else if (_stack_.top().character != '(') {
348 GUM_ERROR(OperationNotAllowed, "expecting '('")
349 }
350
351 _stack_.pop();
352
353 if ((!_stack_.empty()) && _stack_.top().type == FormulaPart::token_type::FUNCTION) {
354 _push_output_(_stack_.top());
355 _stack_.pop();
356 }
358 }

References _last_token_, _push_output_(), _stack_, gum::FormulaPart::FUNCTION, GUM_ERROR, and gum::FormulaPart::PARENTHESIS.

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 392 of file formula_inl.h.

392 {
393 _stack_.push(t);
394 _last_token_ = t;
395 }

References _last_token_, and _stack_.

Referenced by _push_function_(), _push_leftParenthesis_(), and _push_operator_().

Here is the caller 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 315 of file formula_inl.h.

315 {
316 // Only unary operator is the negative sign -
317 FormulaPart t(FormulaPart::token_type::OPERATOR, '_');
319 }

References _push_operator_(), and gum::FormulaPart::OPERATOR.

Referenced by _push_operator_().

Here is the call graph for this function:
Here is the caller 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 445 of file formula_inl.h.

445 {
446 if (_variables_.exists(var)) {
448
449 } else {
450 GUM_ERROR(OperationNotAllowed, "unknonw variable")
451 }
452 }
HashTable< std::string, double > _variables_
The variables available in this formula.
Definition formula.h:479
void _push_number_(const double &v)
Push a number in the formula.

References _push_number_(), _variables_, and GUM_ERROR.

Referenced by _push_identifier_().

Here is the call graph for this function:
Here is the caller 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 371 of file formula_inl.h.

372 {
373 std::vector< FormulaPart > args;
374
375 if (stack.size() < item.argc()) { GUM_ERROR(OperationNotAllowed, "not enought inputs ") }
376
377 while (item.argc() > args.size()) {
378 args.push_back(stack.top());
379 stack.pop();
380 }
381
382 stack.push(item.eval(args));
383 }

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

Referenced by result().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ formula() [1/2]

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

Returns the formula.

Definition at line 263 of file formula_inl.h.

263{ return _formula_; }

References _formula_.

◆ formula() [2/2]

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

Returns the formula.

Definition at line 260 of file formula_inl.h.

260{ return _formula_; }

References _formula_.

◆ operator double()

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

Allows implicit conversion to doubles.

Definition at line 364 of file formula.h.

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

References result().

Here is the call graph for this function:

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

286 {
287 if (this == &source) { return *this; }
288
289 _formula_ = source._formula_;
290 _last_token_ = source._last_token_;
291 _output_ = source._output_;
292 _stack_ = source._stack_;
293
294 _initialise_();
295
296 return *this;
297 }

References Formula(), _formula_, _initialise_(), _last_token_, _output_, and _stack_.

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

299 {
300 if (this == &source) { return *this; }
301
302 _formula_ = std::move(source._formula_);
303 _scanner_ = std::move(source._scanner_);
304 _parser_ = std::move(source._parser_);
305 _parser_->formula(this);
306 _last_token_ = std::move(source._last_token_);
307 _output_ = std::move(source._output_);
308 _stack_ = std::move(source._stack_);
309
310 return *this;
311 }

References Formula(), _formula_, _last_token_, _output_, _parser_, _scanner_, and _stack_.

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

313 {
314 _parser_->Parse();
315
316 std::stack< FormulaPart > stack;
317 if (_output_.empty()) { GUM_ERROR(OperationNotAllowed, "no output found") }
318
319 for (auto item: _output_) {
320 switch (item.type) {
322 stack.push(item);
323 break;
324 }
325
328 _reduceOperatorOrFunction_(item, stack);
329 break;
330 }
331
332 default : {
333 GUM_ERROR(OperationNotAllowed, "expecting numbers, operators or functions")
334 }
335 }
336 }
337
338 if (stack.size() != 1) {
339 GUM_ERROR(OperationNotAllowed, "too many inputs")
340
341 } else if (stack.top().type != FormulaPart::token_type::NUMBER) {
342 GUM_ERROR(OperationNotAllowed, "too many inputs")
343 }
344 return stack.top().number;
345 }
void _reduceOperatorOrFunction_(FormulaPart item, std::stack< FormulaPart > &stack) const
Evaluate an operator or function and push its result.

References _output_, _parser_, _reduceOperatorOrFunction_(), gum::FormulaPart::FUNCTION, GUM_ERROR, gum::FormulaPart::NUMBER, and gum::FormulaPart::OPERATOR.

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

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 439 of file formula_inl.h.

439{ return _variables_; }

References _variables_.

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

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 442 of file formula_inl.h.

442{ return _variables_; }

References _variables_.

◆ gum::formula::Parser

friend class gum::formula::Parser
friend

Definition at line 294 of file formula.h.

References Formula(), and gum::formula::Parser.

Referenced by _initialise_(), and gum::formula::Parser.

Member Data Documentation

◆ _formula_

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

◆ _last_token_

◆ _output_

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

The output stack, will contain one value after evaluation.

Definition at line 473 of file formula.h.

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

◆ _parser_

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

The parser used by the formula.

Definition at line 467 of file formula.h.

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

◆ _scanner_

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

The scanner used by the formula.

Definition at line 464 of file formula.h.

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

◆ _stack_

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

A stack used during evaluation.

Definition at line 476 of file formula.h.

Referenced by Formula(), Formula(), _finalize_(), _popOperator_(), _push_comma_(), _push_operator_(), _push_rightParenthesis_(), _push_stack_(), operator=(), and operator=().

◆ _variables_

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

The variables available in this formula.

Definition at line 479 of file formula.h.

Referenced by _push_variable_(), variables(), and variables().


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