aGrUM 3.0.0
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_ (std::string_view func)
 Push a function in the formula.
void _push_variable_ (std::string_view var)
 Push a variable in the formula.
void _push_identifier_ (std::string_view 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 198 of file formula.cpp.

198 : _formula_(std::to_string(s)), _last_token_(FormulaPart()) {
199 GUM_CONSTRUCTOR(Formula);
200 _initialise_();
201 }
void _initialise_()
Initialise the formula scanner and parser.
Definition formula.cpp:188
std::string _formula_
The formula to evaluate.
Definition formula.h:461
Formula(short s)
Constructor.
Definition formula.cpp:198
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 203 of file formula.cpp.

203 : _formula_(std::to_string(us)), _last_token_(FormulaPart()) {
204 GUM_CONSTRUCTOR(Formula);
205 _initialise_();
206 }

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

208 : _formula_(std::to_string(i)), _last_token_(FormulaPart()) {
209 GUM_CONSTRUCTOR(Formula);
210 _initialise_();
211 }

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

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

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

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

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

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

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

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

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

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

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

239 : _formula_(std::to_string(f)), _last_token_(FormulaPart()) {
240 GUM_CONSTRUCTOR(Formula);
241 _initialise_();
242 }

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

244 : _formula_(std::to_string(d)), _last_token_(FormulaPart()) {
245 GUM_CONSTRUCTOR(Formula);
246 _initialise_();
247 }

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

249 : _formula_(f), _last_token_(FormulaPart()) {
250 GUM_CONSTRUCTOR(Formula);
251
252 _initialise_();
253 }

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

255 :
256 _formula_(source._formula_), _last_token_(source._last_token_), _output_(source._output_),
257 _stack_(source._stack_) {
258 GUM_CONS_CPY(Formula);
259
260 _initialise_();
261 }
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 263 of file formula.cpp.

263 :
264 _formula_(std::move(source._formula_)), _scanner_(std::move(source._scanner_)),
265 _parser_(std::move(source._parser_)), _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::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 272 of file formula.cpp.

272 {
273 GUM_DESTRUCTOR(Formula);
274 ;
275 }

References Formula().

Here is the call graph for this function:

Member Function Documentation

◆ _finalize_()

void gum::Formula::_finalize_ ( )
private

Finalize the formula and prepare it for evaluation.

Definition at line 517 of file formula.cpp.

517 {
518 while (!_stack_.empty()) {
519 if (_stack_.top().character == '(') { GUM_ERROR(OperationNotAllowed, "expecting ')'") }
520
521 _push_output_(_stack_.top());
522 _stack_.pop();
523 }
524 }
void _push_output_(FormulaPart t)
Push the gum::FormulaPart in the output vector.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

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

188 {
189 auto c_str = (unsigned char*)_formula_.c_str();
190 auto scanner = new gum::formula::Scanner(c_str, (int)_formula_.size());
191 _scanner_ = std::unique_ptr< gum::formula::Scanner >(scanner);
192
193 auto parser = new gum::formula::Parser(scanner);
194 _parser_ = std::unique_ptr< gum::formula::Parser >(parser);
195 _parser_->formula(this);
196 }
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 113 of file formula_inl.h.

113 {
114 switch (_last_token_.type) {
118 return o == '-';
119 }
120
122 return (o == '-') && (_last_token_.character == '(');
123 }
124
125 default : {
126 return false;
127 }
128 }
129 }

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

89 {
90 if (_stack_.empty() || _stack_.top().type != FormulaPart::token_type::OPERATOR) {
91 return false;
92 }
93
94 if (o.isLeftAssociative() && o.precedence() <= _stack_.top().precedence()) { return true; }
95
96 if (o.isRightAssociative() && o.precedence() < _stack_.top().precedence()) { return true; }
97
98 return false;
99 }

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

205 {
206 while ((!_stack_.empty()) && (_stack_.top().character != '(')) {
207 _push_output_(_stack_.top());
208 _stack_.pop();
209 }
210
211 if (_stack_.empty() || _stack_.top().character != '(') {
212 GUM_ERROR(OperationNotAllowed, "expecting a '('")
213 }
214
216 }

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

Here is the call graph for this function:

◆ _push_function_()

void gum::Formula::_push_function_ ( std::string_view func)
private

Push a function in the formula.

Parameters
funcThe function to push.

Definition at line 526 of file formula.cpp.

526 {
527 if (func == "exp") {
529 _push_stack_(t);
530
531 } else if (func == "log") {
533 _push_stack_(t);
534
535 } else if (func == "ln") {
537 _push_stack_(t);
538
539 } else if (func == "pow") {
541 _push_stack_(t);
542
543 } else if (func == "sqrt") {
545 _push_stack_(t);
546
547 } else {
548 GUM_ERROR(OperationNotAllowed, "unknown function")
549 }
550 }
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_()

void gum::Formula::_push_identifier_ ( std::string_view ident)
private

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

Definition at line 552 of file formula.cpp.

552 {
553 try {
554 _push_function_(ident);
555
556 } catch (OperationNotAllowed const&) {
557 try {
558 _push_variable_(ident);
559
560 } catch (OperationNotAllowed const&) { GUM_ERROR(OperationNotAllowed, "unknown identifier") }
561 }
562 }
void _push_function_(std::string_view func)
Push a function in the formula.
Definition formula.cpp:526
void _push_variable_(std::string_view var)
Push a variable 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 149 of file formula_inl.h.

149 {
150 FormulaPart t(FormulaPart::token_type::PARENTHESIS, '(');
151 _push_stack_(t);
152 }

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

83 {
84 FormulaPart t(FormulaPart::token_type::NUMBER, v);
86 }

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

102 {
103 if (_isUnaryOperator_(o)) {
105
106 } else {
107 FormulaPart t(FormulaPart::token_type::OPERATOR, o);
109 }
110 }
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 139 of file formula_inl.h.

139 {
140 while (_popOperator_(t)) {
141 _push_output_(_stack_.top());
142 _stack_.pop();
143 }
144
145 _push_stack_(t);
146 }
bool _popOperator_(FormulaPart o)
Pop the operator in the inner formula's stack.
Definition formula_inl.h:89

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

193 {
194 _output_.push_back(t);
195 _last_token_ = t;
196 }

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

155 {
156 while ((!_stack_.empty()) && (_stack_.top().character != '(')) {
157 _push_output_(_stack_.top());
158 _stack_.pop();
159 }
160
161 if (_stack_.empty()) {
162 GUM_ERROR(OperationNotAllowed, "expecting '('")
163
164 } else if (_stack_.top().character != '(') {
165 GUM_ERROR(OperationNotAllowed, "expecting '('")
166 }
167
168 _stack_.pop();
169
170 if ((!_stack_.empty()) && _stack_.top().type == FormulaPart::token_type::FUNCTION) {
171 _push_output_(_stack_.top());
172 _stack_.pop();
173 }
175 }

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

199 {
200 _stack_.push(t);
201 _last_token_ = t;
202 }

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

132 {
133 // Only unary operator is the negative sign -
134 FormulaPart t(FormulaPart::token_type::OPERATOR, '_');
136 }

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_ ( std::string_view var)
private

Push a variable in the formula.

Definition at line 225 of file formula_inl.h.

225 {
226 if (_variables_.exists(var)) {
228
229 } else {
230 GUM_ERROR(OperationNotAllowed, "unknonw variable")
231 }
232 }
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.
Definition formula_inl.h:83

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

179 {
180 std::vector< FormulaPart > args;
181
182 if (stack.size() < item.argc()) { GUM_ERROR(OperationNotAllowed, "not enought inputs ") }
183
184 while (item.argc() > args.size()) {
185 args.push_back(stack.top());
186 stack.pop();
187 }
188
189 stack.push(item.eval(args));
190 }

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

80{ return _formula_; }

References _formula_.

◆ formula() [2/2]

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

Returns the formula.

Args are backwards !

Args are backwards ! Args are backwards !

Definition at line 77 of file formula_inl.h.

77{ return _formula_; }

References _formula_.

◆ operator double()

INLINE gum::Formula::operator double ( ) const
explicit

Allows implicit conversion to doubles.

Definition at line 271 of file formula_inl.h.

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

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

277 {
278 if (this == &source) { return *this; }
279
280 _formula_ = source._formula_;
281 _last_token_ = source._last_token_;
282 _output_ = source._output_;
283 _stack_ = source._stack_;
284
285 _initialise_();
286
287 return *this;
288 }

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

290 {
291 if (this == &source) { return *this; }
292
293 _formula_ = std::move(source._formula_);
294 _scanner_ = std::move(source._scanner_);
295 _parser_ = std::move(source._parser_);
296 _parser_->formula(this);
297 _last_token_ = std::move(source._last_token_);
298 _output_ = std::move(source._output_);
299 _stack_ = std::move(source._stack_);
300
301 return *this;
302 }

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

304 {
305 _parser_->Parse();
306
307 std::stack< FormulaPart > stack;
308 if (_output_.empty()) { GUM_ERROR(OperationNotAllowed, "no output found") }
309
310 for (auto item: _output_) {
311 switch (item.type) {
313 stack.push(item);
314 break;
315 }
316
319 _reduceOperatorOrFunction_(item, stack);
320 break;
321 }
322
323 default : {
324 GUM_ERROR(OperationNotAllowed, "expecting numbers, operators or functions")
325 }
326 }
327 }
328
329 if (stack.size() != 1) {
330 GUM_ERROR(OperationNotAllowed, "too many inputs")
331
332 } else if (stack.top().type != FormulaPart::token_type::NUMBER) {
333 GUM_ERROR(OperationNotAllowed, "too many inputs")
334 }
335 return stack.top().number;
336 }
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 219 of file formula_inl.h.

219{ 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 222 of file formula_inl.h.

222{ 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: