Class calc_utf8 (o2scl)

O2scl : Class List

template<class fp_t = double>
class calc_utf8

Evaluate a mathematical expression stored in a UTF8 string.

This is based on Brandon Amos’ code at https://github.com/bamos/cpp-expression-parser in turn based on Jesse Brown’s code at http://www.daniweb.com/software-development/cpp/code/427500/calculator-using-shunting-yard-algorithm .

The original code has been modified for use in O₂scl.

Todo

In class calc_utf8:

  • Future: Add more special functions. Add hypot(x,y,z)?

  • Future: There is some code duplication across the functions, especially with regard to conversion between UTF8 and char32, which could be removed.

Token types

std::map<std::string, int> op_precedence

A map denoting operator precedence.

token_queue_t RPN

The current expression in RPN.

static const int token_none = 0
static const int token_op = 1
static const int token_var = 2
static const int token_num = 3
bool allow_min

If true, interpret “min” as the minimum function (default true)

This setting allows convert_units to use this class without creating confusion between “minutes” and the “minimum” function.

inline std::map<std::string, int> build_op_precedence()

Build the operator precedence map.

inline bool is_variable_char(const char32_t c)

Return true if is a variable.

inline void cleanRPN(token_queue_t &rpn)

Empty and free memory associated with rpn.

Note

This is called by the destructor to free the memory in RPN .

inline int toRPN_nothrow(const std::u32string &expr, const std::map<std::u32string, fp_t> *vars, std::map<std::string, int> op_prec, token_queue_t &rpn_queue2)

Convert the expression in expr to RPN and return an integer to indicate success or failure.

inline int calc_RPN_nothrow(token_queue_t rpn, const std::map<std::u32string, fp_t> *vars, fp_t &result)

Compile and evaluate the expression in RPN using definitions in vars and return an integer to indicate success or failure.

inline calc_utf8()

Create an empty calc_utf8 object.

inline calc_utf8(const std::u32string &expr, const std::map<std::u32string, fp_t> *vars = 0)

Compile expression expr using variables specified in vars.

inline ~calc_utf8()
inline void set_rng(rng<> &r_new)

Set the random number generator.

Evaluate functions

int verbose

Verbosity parameter.

inline fp_t eval_char32(const std::map<std::u32string, fp_t> *vars = 0)

Evalate the previously compiled expression using variables specified in vars.

inline int eval_char32_nothrow(const std::map<std::u32string, fp_t> *vars, fp_t &result)

Evalate the previously compiled expression using variables specified in vars.

inline fp_t eval(const std::map<std::string, fp_t> *vars = 0)

Evalate the previously compiled expression using variables specified in vars.

inline int eval_nothrow(const std::map<std::string, fp_t> *vars, fp_t &result)

Evalate the previously compiled expression using variables specified in vars.

Compile and evaluate

inline fp_t calculate(const std::u32string &expr, const std::map<std::u32string, fp_t> *vars = 0)

Compile and evaluate expr using definitions in vars.

inline fp_t calculate(const std::string &expr, const std::map<std::string, fp_t> *vars = 0)

Compile and evaluate expr using definitions in vars.

inline int calculate_nothrow(const std::u32string &expr, const std::map<std::u32string, fp_t> *vars, fp_t &result)

Compile and evaluate expr using definitions in vars and return an integer to indicate success or failure.

inline int calculate_nothrow(const std::string &expr, const std::map<std::string, fp_t> *vars, fp_t &result)

Compile and evaluate expr using definitions in vars and return an integer to indicate success or failure.

Compile functions

inline void compile(const std::u32string &expr, const std::map<std::u32string, fp_t> *vars = 0)

Compile expression expr using variables specified in vars and return an integer to indicate success or failure.

inline int compile_nothrow(const std::u32string &expr, const std::map<std::u32string, fp_t> *vars = 0)

Compile expression expr using variables specified in vars and return an integer to indicate success or failure.

inline void compile(const std::string &expr, const std::map<std::string, fp_t> *vars = 0)

Compile expression expr using variables specified in vars and return an integer to indicate success or failure.

inline int compile_nothrow(const std::string &expr, const std::map<std::string, fp_t> *vars = 0)

Compile expression expr using variables specified in vars and return an integer to indicate success or failure.

Other functions

inline std::string RPN_to_string()

Convert the RPN expression to a string.

Note

This is mostly useful for debugging

inline token_queue_t get_RPN()

Get a copy of the RPN version.

Warning

This copy contains raw pointers which are invalid if changed.

inline std::vector<std::u32string> get_var_list()

Get the variable list.

calc_utf8(const calc_utf8&)
calc_utf8 &operator=(const calc_utf8&)

Protected Types

typedef std::queue<token_base*> token_queue_t

A typedef for a queue of tokens for o2scl::calc_utf8.

Protected Attributes

rng *r

Pointer to a random number generator for rand.

rng def_r

The default random number generator for rand.

polylog_multip<fp_t, fp_t> pm

Object for computing Fermi-Dirac integrals.

template<class T>
class token32 : public o2scl::calc_utf8<fp_t>::token_base

Token class for o2scl::calc_utf8.

Public Functions

inline token32(T t, int typex)

Create a token of type type with value t.

Public Members

T val

The actual value stored.

struct token_base

Token base data type for o2scl::calc_utf8.

Subclassed by o2scl::calc_utf8< fp_t >::token32< T >

Public Functions

inline virtual ~token_base()

Public Members

int type

The token type.