ʻOhana
Population structure, admixture history, and selection using learning methods.
Data Structures | Public Types | Public Member Functions
jade::basic_shunting_yard< TFloat > Class Template Reference

A template for a class that implements the shunting-yard algorithm. The implementation supports '+', '-', '*', and '/' operators; the '(' and ')' parentheses; floating-point numbers; and variables. After parsing an expression, the instance can evaluate the expression if given a table of variable values. More...

#include <jade.shunting_yard.hpp>

+ Collaboration diagram for jade::basic_shunting_yard< TFloat >:

Public Types

typedef TFloat float_type
 The floating-point type. More...
 
typedef std::map< std::string, float_typeargs_type
 A map of strings to floating-point values. The string represent the variable names parsed from the expression passed to the constructor, and the floating-point values are used to evaluate the expression. More...
 

Public Member Functions

 basic_shunting_yard (std::istream &in)
 Initializes a new instance of the class. The specified input stream provides tokens for the expression. More...
 
 basic_shunting_yard (const std::string &expression)
 Initializes a new instance of the class. The specified string provides tokens for the expression. More...
 
const args_typeget_args () const
 Returns a table of arguments that maps variables to values. The table reference returned has all values set to zero. The table can be used to evaluate the expression. More...
 
float_type evaluate (const args_type &args=args_type()) const
 Evaluates the expression using values from the specified table. More...
 
float_type operator() (const args_type &args=args_type()) const
 Evaluates the expression using values from the specified table. More...
 

Detailed Description

template<typename TFloat>
class jade::basic_shunting_yard< TFloat >

A template for a class that implements the shunting-yard algorithm. The implementation supports '+', '-', '*', and '/' operators; the '(' and ')' parentheses; floating-point numbers; and variables. After parsing an expression, the instance can evaluate the expression if given a table of variable values.

Definition at line 22 of file jade.shunting_yard.hpp.

Member Typedef Documentation

◆ args_type

template<typename TFloat >
typedef std::map<std::string, float_type> jade::basic_shunting_yard< TFloat >::args_type

A map of strings to floating-point values. The string represent the variable names parsed from the expression passed to the constructor, and the floating-point values are used to evaluate the expression.

Definition at line 33 of file jade.shunting_yard.hpp.

◆ float_type

template<typename TFloat >
typedef TFloat jade::basic_shunting_yard< TFloat >::float_type

The floating-point type.

Definition at line 25 of file jade.shunting_yard.hpp.

Constructor & Destructor Documentation

◆ basic_shunting_yard() [1/2]

template<typename TFloat >
jade::basic_shunting_yard< TFloat >::basic_shunting_yard ( std::istream &  in)
inlineexplicit

Initializes a new instance of the class. The specified input stream provides tokens for the expression.

Parameters
inThe input stream providing tokens.

Definition at line 40 of file jade.shunting_yard.hpp.

41  : _args ()
42  , _queue ()
43  {
44  std::vector<_token> tokens;
45  _scan(in, tokens);
46  _enqueue(tokens);
47 
48  for (const auto & t : _queue)
49  if (t.type == _token::variable)
50  _args[t.text] = float_type(0);
51 
52  evaluate(_args);
53  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ basic_shunting_yard() [2/2]

template<typename TFloat >
jade::basic_shunting_yard< TFloat >::basic_shunting_yard ( const std::string &  expression)
inlineexplicit

Initializes a new instance of the class. The specified string provides tokens for the expression.

Parameters
expressionThe string providing tokens.

Definition at line 60 of file jade.shunting_yard.hpp.

61  : _args ()
62  , _queue ()
63  {
64  std::istringstream in (expression);
65  *this = basic_shunting_yard(in);
66  }
+ Here is the call graph for this function:

Member Function Documentation

◆ evaluate()

template<typename TFloat >
float_type jade::basic_shunting_yard< TFloat >::evaluate ( const args_type args = args_type()) const
inline

Evaluates the expression using values from the specified table.

Parameters
argsThe table of variable values.
Returns
The output from the expression.

Definition at line 84 of file jade.shunting_yard.hpp.

85  {
86  std::vector<float_type> stack;
87 
88  for (const auto & t : _queue)
89  {
90  if (t.type == _token::number)
91  {
92  stack.push_back(t.get_value());
93  }
94  else if (t.type == _token::variable)
95  {
96  const auto item = args.find(t.text);
97 
98  if (item == args.end())
99  throw jade::error()
100  << "undefined variable '"
101  << t.text
102  << "'";
103 
104  stack.push_back(item->second);
105  }
106  else
107  {
108  if (stack.size() < 2)
109  throw jade::error("invalid expression");
110 
111  const auto n2 = stack.back();
112  stack.pop_back();
113  const auto n1 = stack.back();
114  stack.pop_back();
115 
116  if (t.type == _token::plus ) stack.push_back(n1 + n2);
117  else if (t.type == _token::minus) stack.push_back(n1 - n2);
118  else if (t.type == _token::star ) stack.push_back(n1 * n2);
119  else if (t.type == _token::slash) stack.push_back(n1 / n2);
120  else throw jade::error("invalid operator");
121  }
122  }
123 
124  if (stack.size() != 1)
125  throw jade::error("invalid expression");
126 
127  return stack[0];
128  }
+ Here is the caller graph for this function:

◆ get_args()

template<typename TFloat >
const args_type& jade::basic_shunting_yard< TFloat >::get_args ( ) const
inline

Returns a table of arguments that maps variables to values. The table reference returned has all values set to zero. The table can be used to evaluate the expression.

Returns
A table mapping variables to values.

Definition at line 74 of file jade.shunting_yard.hpp.

75  {
76  return _args;
77  }

◆ operator()()

template<typename TFloat >
float_type jade::basic_shunting_yard< TFloat >::operator() ( const args_type args = args_type()) const
inline

Evaluates the expression using values from the specified table.

Parameters
argsThe table of variable values.
Returns
The output from the expression.

Definition at line 135 of file jade.shunting_yard.hpp.

138  {
139  return evaluate(args);
140  }
+ Here is the call graph for this function:

The documentation for this class was generated from the following file:
jade::basic_shunting_yard::float_type
TFloat float_type
The floating-point type.
Definition: jade.shunting_yard.hpp:25
jade::basic_shunting_yard::basic_shunting_yard
basic_shunting_yard(std::istream &in)
Initializes a new instance of the class. The specified input stream provides tokens for the expressio...
Definition: jade.shunting_yard.hpp:40
jade::basic_shunting_yard::evaluate
float_type evaluate(const args_type &args=args_type()) const
Evaluates the expression using values from the specified table.
Definition: jade.shunting_yard.hpp:84
jade::basic_error
A template for a class representing an exception thrown from this namespace.
Definition: jade.error.hpp:20