DataTree.hh 4.37 KB
Newer Older
1
2
#ifndef _DATATREE_HH
#define _DATATREE_HH
sebastien's avatar
sebastien committed
3
4
5

using namespace std;

6
7
#include <string>
#include <map>
sebastien's avatar
sebastien committed
8
9
#include <list>

10
11
12
#include "SymbolTable.hh"
#include "NumericalConstants.hh"
#include "VariableTable.hh"
sebastien's avatar
sebastien committed
13
#include "ExprNode.hh"
14
15
16

class DataTree
{
sebastien's avatar
sebastien committed
17
18
19
20
21
22
  friend class ExprNode;
  friend class NumConstNode;
  friend class VariableNode;
  friend class UnaryOpNode;
  friend class BinaryOpNode;
protected:
23
24
  //! A reference to the symbol table
  SymbolTable &symbol_table;
sebastien's avatar
sebastien committed
25
26
27
28
29
30
31
32
33
  //! Reference to numerical constants table
  NumericalConstants &num_constants;
  
  typedef list<NodeID> node_list_type;
  //! The list of nodes
  node_list_type node_list;
  //! A counter for filling ExprNode's idx field
  int node_counter;

sebastien's avatar
sebastien committed
34
35
  //! Stores local variables value
  map<int, NodeID> local_variables_table;
sebastien's avatar
sebastien committed
36
37
38

  typedef map<int, NodeID> num_const_node_map_type;
  num_const_node_map_type num_const_node_map;
sebastien's avatar
sebastien committed
39
40
  //! Type (symbol_id, type, lag) used as key
  typedef map<pair<pair<int, Type>, int>, NodeID> variable_node_map_type;
sebastien's avatar
sebastien committed
41
42
43
44
45
46
47
48
49
50
51
  variable_node_map_type variable_node_map;
  typedef map<pair<NodeID, int>, NodeID> unary_op_node_map_type;
  unary_op_node_map_type unary_op_node_map;
  typedef map<pair<pair<NodeID, NodeID>, int>, NodeID> binary_op_node_map_type;
  binary_op_node_map_type binary_op_node_map;

  inline NodeID AddUnaryOp(UnaryOpcode op_code, NodeID arg);
  inline NodeID AddBinaryOp(NodeID arg1, BinaryOpcode op_code, NodeID arg2);
public:
  DataTree(SymbolTable &symbol_table_arg, NumericalConstants &num_constants_arg);
  virtual ~DataTree();
sebastien's avatar
sebastien committed
52
53
  //! The variable table
  VariableTable variable_table;
sebastien's avatar
sebastien committed
54
  NodeID Zero, One, MinusOne;
sebastien's avatar
sebastien committed
55

sebastien's avatar
sebastien committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  //! Raised when a local parameter is declared twice
  class LocalParameterException
  {
  public:
    string name;
    LocalParameterException(const string &name_arg) : name(name_arg) {}
  };

  NodeID AddNumConstant(const string &value);
  NodeID AddVariable(const string &name, int lag = 0);
  //! Adds "arg1+arg2" to model tree
  NodeID AddPlus(NodeID iArg1, NodeID iArg2);
  //! Adds "arg1-arg2" to model tree
  NodeID AddMinus(NodeID iArg1, NodeID iArg2);
  //! Adds "-arg" to model tree
  NodeID AddUMinus(NodeID iArg1);
  //! Adds "arg1*arg2" to model tree
  NodeID AddTimes(NodeID iArg1, NodeID iArg2);
  //! Adds "arg1/arg2" to model tree
  NodeID AddDivide(NodeID iArg1, NodeID iArg2);
  //! Adds "arg1^arg2" to model tree
  NodeID AddPower(NodeID iArg1, NodeID iArg2);
  //! Adds "exp(arg)" to model tree
  NodeID AddExp(NodeID iArg1);
  //! Adds "log(arg)" to model tree
  NodeID AddLog(NodeID iArg1);
  //! Adds "log10(arg)" to model tree
  NodeID AddLog10(NodeID iArg1);
  //! Adds "cos(arg)" to model tree
  NodeID AddCos(NodeID iArg1);
  //! Adds "sin(arg)" to model tree
  NodeID AddSin(NodeID iArg1);
  //! Adds "tan(arg)" to model tree
  NodeID AddTan(NodeID iArg1);
  //! Adds "acos(arg)" to model tree
  NodeID AddACos(NodeID iArg1);
  //! Adds "asin(arg)" to model tree
  NodeID AddASin(NodeID iArg1);
  //! Adds "atan(arg)" to model tree
  NodeID AddATan(NodeID iArg1);
  //! Adds "cosh(arg)" to model tree
  NodeID AddCosH(NodeID iArg1);
  //! Adds "sinh(arg)" to model tree
  NodeID AddSinH(NodeID iArg1);
  //! Adds "tanh(arg)" to model tree
  NodeID AddTanH(NodeID iArg1);
  //! Adds "acosh(arg)" to model tree
  NodeID AddACosH(NodeID iArg1);
  //! Adds "asinh(arg)" to model tree
  NodeID AddASinH(NodeID iArg1);
  //! Adds "atanh(args)" to model tree
  NodeID AddATanH(NodeID iArg1);
  //! Adds "sqrt(arg)" to model tree
  NodeID AddSqRt(NodeID iArg1);
  //! Adds "arg1=arg2" to model tree
  NodeID AddEqual(NodeID iArg1, NodeID iArg2);
  void AddLocalParameter(const string &name, NodeID value) throw (LocalParameterException);
sebastien's avatar
sebastien committed
113
114
115
  //! Adds an unknown function node
  /*! \todo Use a map to share identical nodes */
  NodeID AddUnknownFunction(const string &function_name, const vector<NodeID> &arguments);
sebastien's avatar
sebastien committed
116
};
117

sebastien's avatar
sebastien committed
118
119
inline NodeID
DataTree::AddUnaryOp(UnaryOpcode op_code, NodeID arg)
120
{
sebastien's avatar
sebastien committed
121
122
123
  unary_op_node_map_type::iterator it = unary_op_node_map.find(make_pair(arg, op_code));
  if (it != unary_op_node_map.end())
    return it->second;
124
  else
sebastien's avatar
sebastien committed
125
    return new UnaryOpNode(*this, op_code, arg);
126
127
}

sebastien's avatar
sebastien committed
128
129
inline NodeID
DataTree::AddBinaryOp(NodeID arg1, BinaryOpcode op_code, NodeID arg2)
130
{
sebastien's avatar
sebastien committed
131
132
133
  binary_op_node_map_type::iterator it = binary_op_node_map.find(make_pair(make_pair(arg1, arg2), op_code));
  if (it != binary_op_node_map.end())
    return it->second;
134
  else
sebastien's avatar
sebastien committed
135
    return new BinaryOpNode(*this, arg1, op_code, arg2);
136
137
138
}

#endif