aGrUM 3.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:
[legend]

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
Formula & operator= (const Formula &source)
 Copy operator.
Formula & operator= (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 204 of file formula.cpp.

204 : _formula_(std::to_string(s)), _last_token_(FormulaPart()) {
205 GUM_CONSTRUCTOR(Formula);
206 _initialise_();
207 }
FormulaPart()
Class constructor.
Definition formula.cpp:111
void _initialise_()
Initialise the formula scanner and parser.
Definition formula.cpp:194
std::string _formula_
The formula to evaluate.
Definition formula.h:461
Formula(short s)
Constructor.
Definition formula.cpp:204
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, gum::operator*(), gum::operator+(), gum::operator-(), gum::operator-(), gum::operator/(), operator=(), operator=(), and gum::prm::o3prm::O3Formula::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 209 of file formula.cpp.

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

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

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

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

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

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

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

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

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

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

234 : _formula_(std::to_string(l)), _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() [8/13]

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

Constructor.

Definition at line 239 of file formula.cpp.

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

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

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

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

250 : _formula_(std::to_string(d)), _last_token_(FormulaPart()) {
251 GUM_CONSTRUCTOR(Formula);
252 _initialise_();
253 }

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

256 GUM_CONSTRUCTOR(Formula);
257
258 _initialise_();
259 }

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

261 :
262 _formula_(source._formula_), _last_token_(source._last_token_), _output_(source._output_),
263 _stack_(source._stack_) {
264 GUM_CONS_CPY(Formula);
265
266 _initialise_();
267 }
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 269 of file formula.cpp.

269 :
270 _formula_(std::move(source._formula_)), _scanner_(std::move(source._scanner_)),
271 _parser_(std::move(source._parser_)), _last_token_(std::move(source._last_token_)),
272 _output_(std::move(source._output_)), _stack_(std::move(source._stack_)) {
273 GUM_CONS_CPY(Formula);
274
275 _parser_->formula(this);
276 }
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 278 of file formula.cpp.

278 {
279 GUM_DESTRUCTOR(Formula);
280 ;
281 }

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

523 {
524 while (!_stack_.empty()) {
525 if (_stack_.top().character == '(') { GUM_ERROR(OperationNotAllowed, "expecting ')'") }
526
527 _push_output_(_stack_.top());
528 _stack_.pop();
529 }
530 }
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.

Referenced by gum::formula::Parser.

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

◆ _initialise_()

void gum::Formula::_initialise_ ( )
private

Initialise the formula scanner and parser.

Definition at line 194 of file formula.cpp.

194 {
195 auto c_str = (unsigned char*)_formula_.c_str();
196 auto scanner = new gum::formula::Scanner(c_str, (int)_formula_.size());
197 _scanner_ = std::unique_ptr< gum::formula::Scanner >(scanner);
198
199 auto parser = new gum::formula::Parser(scanner);
200 _parser_ = std::unique_ptr< gum::formula::Parser >(parser);
201 _parser_->formula(this);
202 }
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 }
char character
The value stored by this gum::FormulaPart.
Definition formula.h:98
token_type type
The token_type stored by this gum::FormulaPart.
Definition formula.h:88

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

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 gum::FormulaPart::FormulaPart(), _last_token_, _push_output_(), _stack_, gum::FormulaPart::ARG_SEP, and GUM_ERROR.

Referenced by gum::formula::Parser.

Here is the call graph for this function:
Here is the caller 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 532 of file formula.cpp.

532 {
533 if (func == "exp") {
535 _push_stack_(t);
536
537 } else if (func == "log") {
539 _push_stack_(t);
540
541 } else if (func == "ln") {
543 _push_stack_(t);
544
545 } else if (func == "pow") {
547 _push_stack_(t);
548
549 } else if (func == "sqrt") {
551 _push_stack_(t);
552
553 } else {
554 GUM_ERROR(OperationNotAllowed, "unknown function")
555 }
556 }
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_(), and gum::formula::Parser.

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

558 {
559 try {
560 _push_function_(ident);
561
562 } catch (OperationNotAllowed const&) {
563 try {
564 _push_variable_(ident);
565
566 } catch (OperationNotAllowed const&) { GUM_ERROR(OperationNotAllowed, "unknown identifier") }
567 }
568 }
void _push_function_(std::string_view func)
Push a function in the formula.
Definition formula.cpp:532
void _push_variable_(std::string_view var)
Push a variable in the formula.

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

Referenced by gum::formula::Parser.

Here is the call graph for this function:
Here is the caller 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.

Referenced by gum::formula::Parser.

Here is the call graph for this function:
Here is the caller 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_(), and gum::formula::Parser.

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_(), _push_unaryOperator_(), and gum::formula::Parser.

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 gum::FormulaPart::FormulaPart(), _last_token_, _push_output_(), _stack_, gum::FormulaPart::FUNCTION, GUM_ERROR, and gum::FormulaPart::PARENTHESIS.

Referenced by gum::formula::Parser.

Here is the call graph for this function:
Here is the caller 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
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.

References _push_number_(), _variables_, gum::HashTable< Key, Val >::exists(), and GUM_ERROR.

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

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_.

Referenced by gum::formula::Parser.

Here is the caller graph for this function:

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

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

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

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

Referenced by gum::formula::Parser.

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

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

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

310 {
311 _parser_->Parse();
312
313 std::stack< FormulaPart > stack;
314 if (_output_.empty()) { GUM_ERROR(OperationNotAllowed, "no output found") }
315
316 for (auto item: _output_) {
317 switch (item.type) {
319 stack.push(item);
320 break;
321 }
322
325 _reduceOperatorOrFunction_(item, stack);
326 break;
327 }
328
329 default : {
330 GUM_ERROR(OperationNotAllowed, "expecting numbers, operators or functions")
331 }
332 }
333 }
334
335 if (stack.size() != 1) {
336 GUM_ERROR(OperationNotAllowed, "too many inputs")
337
338 } else if (stack.top().type != FormulaPart::token_type::NUMBER) {
339 GUM_ERROR(OperationNotAllowed, "too many inputs")
340 }
341 return stack.top().number;
342 }
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_(), gum::formula::Parser, 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_(), and gum::formula::Parser.

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

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: