gem5  v20.1.0.0
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
MathExpr Class Reference

#include <mathexpr.hh>

Classes

class  Node
 
struct  OpSearch
 

Public Types

typedef std::function< double(std::string)> EvalCallback
 

Public Member Functions

 MathExpr (std::string expr)
 
std::string toStr () const
 Prints an ASCII representation of the expression tree. More...
 
double eval (EvalCallback fn) const
 Evaluates the expression. More...
 
std::vector< std::string > getVariables () const
 Return all variables in the this expression. More...
 

Private Types

enum  Operator {
  bAdd, bSub, bMul, bDiv,
  bPow, uNeg, sValue, sVariable,
  nInvalid
}
 
typedef double(* binOp) (double, double)
 

Private Member Functions

Nodeparse (std::string expr)
 Parse and create nodes from string. More...
 
std::string toStr (Node *n, std::string prefix) const
 Print tree as string. More...
 
double eval (const Node *n, EvalCallback fn) const
 Eval a node. More...
 
void getVariables (const Node *n, std::vector< std::string > &vars) const
 Return all variable reachable from a node to a vector of strings. More...
 

Private Attributes

const int MAX_PRIO = 4
 
std::array< OpSearch, uNeg+1 > ops
 Operator list. More...
 
Noderoot
 Root node. More...
 

Detailed Description

Definition at line 47 of file mathexpr.hh.

Member Typedef Documentation

◆ binOp

typedef double(* MathExpr::binOp) (double, double)
private

Definition at line 93 of file mathexpr.hh.

◆ EvalCallback

typedef std::function<double(std::string)> MathExpr::EvalCallback

Definition at line 52 of file mathexpr.hh.

Member Enumeration Documentation

◆ Operator

enum MathExpr::Operator
private
Enumerator
bAdd 
bSub 
bMul 
bDiv 
bPow 
uNeg 
sValue 
sVariable 
nInvalid 

Definition at line 87 of file mathexpr.hh.

Constructor & Destructor Documentation

◆ MathExpr()

MathExpr::MathExpr ( std::string  expr)

Definition at line 47 of file mathexpr.cc.

References ArmISA::a, ArmISA::b, bAdd, bDiv, bMul, bPow, bSub, and uNeg.

Member Function Documentation

◆ eval() [1/2]

double MathExpr::eval ( const Node n,
EvalCallback  fn 
) const
private

Eval a node.

Definition at line 150 of file mathexpr.cc.

References eval(), ArmISA::fn, ArmISA::n, ops, panic, sValue, and sVariable.

◆ eval() [2/2]

double MathExpr::eval ( EvalCallback  fn) const
inline

Evaluates the expression.

Parameters
fnA callback funcion to evaluate variables
Returns
The value for this expression

Definition at line 68 of file mathexpr.hh.

References eval(), ArmISA::fn, and root.

Referenced by MathExprPowerModel::eval(), and eval().

◆ getVariables() [1/2]

std::vector<std::string> MathExpr::getVariables ( ) const
inline

Return all variables in the this expression.

This function starts from the root node and traverses all nodes while adding the variables it finds to a vector. Returns the found variables in a vector of strings

Returns
A Vector with the names of all variables

Definition at line 79 of file mathexpr.hh.

References root.

Referenced by getVariables().

◆ getVariables() [2/2]

void MathExpr::getVariables ( const Node n,
std::vector< std::string > &  vars 
) const
private

Return all variable reachable from a node to a vector of strings.

Definition at line 178 of file mathexpr.cc.

References getVariables(), ArmISA::n, nInvalid, sValue, and sVariable.

◆ parse()

MathExpr::Node * MathExpr::parse ( std::string  expr)
private

Parse and create nodes from string.

This function parses a string expression into an expression tree.

It will look for operators in priority order to recursively build the tree, respecting parenthesization. Constants can be expressed in any format accepted by std::stod, whereas variables are essentially [A-Za-z0-9.$\]+

Definition at line 75 of file mathexpr.cc.

References ArmISA::c, ArmISA::i, MipsISA::l, MAX_PRIO, ArmISA::n, ops, MipsISA::p, MipsISA::r, sValue, sVariable, and ArmISA::v.

◆ toStr() [1/2]

std::string MathExpr::toStr ( ) const
inline

Prints an ASCII representation of the expression tree.

Returns
A string containing the ASCII representation of the expression

Definition at line 59 of file mathexpr.hh.

References root, and toStr().

Referenced by toStr().

◆ toStr() [2/2]

std::string MathExpr::toStr ( Node n,
std::string  prefix 
) const
private

Print tree as string.

Definition at line 167 of file mathexpr.cc.

References ArmISA::n, and toStr().

Member Data Documentation

◆ MAX_PRIO

const int MathExpr::MAX_PRIO = 4
private

Definition at line 92 of file mathexpr.hh.

Referenced by parse().

◆ ops

std::array<OpSearch, uNeg + 1> MathExpr::ops
private

Operator list.

Definition at line 103 of file mathexpr.hh.

Referenced by eval(), and parse().

◆ root

Node* MathExpr::root
private

Root node.

Definition at line 129 of file mathexpr.hh.

Referenced by eval(), getVariables(), and toStr().


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

Generated on Wed Sep 30 2020 14:02:27 for gem5 by doxygen 1.8.17