v4 parser VariableTable:
* cleaned and simplified the code * incorporated VariableTable as a private member of DataTree/ModelTree git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1118 ac1d8469-bf42-47a9-8791-bf33cf982152time-shift
parent
4f9e8a6e27
commit
be18e12a43
|
@ -17,9 +17,9 @@ using namespace std;
|
|||
#include "NumericalConstants.hh"
|
||||
#include "DataTree.hh"
|
||||
|
||||
DataTree::DataTree(SymbolTable &symbol_table_arg, VariableTable &variable_table_arg) :
|
||||
DataTree::DataTree(SymbolTable &symbol_table_arg, ModelParameters &mod_param_arg) :
|
||||
symbol_table(symbol_table_arg),
|
||||
variable_table(variable_table_arg),
|
||||
variable_table(symbol_table_arg, mod_param_arg),
|
||||
NoOpCode(-1), NullID(NULL)
|
||||
{
|
||||
offset = 1;
|
||||
|
|
|
@ -2,8 +2,7 @@
|
|||
#include "Interface.hh"
|
||||
|
||||
ModFile::ModFile() : symbol_table(model_parameters),
|
||||
variable_table(symbol_table, model_parameters),
|
||||
model_tree(symbol_table, variable_table, model_parameters, num_constants),
|
||||
model_tree(symbol_table, model_parameters, num_constants),
|
||||
order(-1), linear(-1)
|
||||
{
|
||||
}
|
||||
|
|
|
@ -34,9 +34,10 @@ inline NodeID MetaToken::getDerivativeAddress(int iVarID, const ModelTree &model
|
|||
return iter->second;
|
||||
}
|
||||
|
||||
ModelTree::ModelTree(SymbolTable &symbol_table_arg, VariableTable &variable_table_arg,
|
||||
ModelParameters &mod_param_arg, const NumericalConstants &num_constants_arg) :
|
||||
DataTree(symbol_table_arg, variable_table_arg),
|
||||
ModelTree::ModelTree(SymbolTable &symbol_table_arg,
|
||||
ModelParameters &mod_param_arg,
|
||||
const NumericalConstants &num_constants_arg) :
|
||||
DataTree(symbol_table_arg, mod_param_arg),
|
||||
mod_param(mod_param_arg),
|
||||
num_constants(num_constants_arg),
|
||||
computeHessian(false),
|
||||
|
|
|
@ -44,7 +44,6 @@ ParsingDriver::parse(const string &f)
|
|||
|
||||
mod_file->model_tree.error = error;
|
||||
mod_file->symbol_table.error = error;
|
||||
mod_file->variable_table.error = error;
|
||||
|
||||
expression.setNumericalConstants(&mod_file->num_constants);
|
||||
tmp_symbol_table = new TmpSymbolTable(mod_file->symbol_table);
|
||||
|
@ -128,13 +127,6 @@ NodeID
|
|||
ParsingDriver::add_model_variable(string *name)
|
||||
{
|
||||
check_symbol_existence(*name);
|
||||
Type type = mod_file->symbol_table.getType(*name);
|
||||
|
||||
if ((type == eEndogenous)
|
||||
|| (type == eExogenous)
|
||||
|| (type == eExogenousDet))
|
||||
mod_file->variable_table.AddVariable(*name, 0);
|
||||
|
||||
NodeID id = mod_file->model_tree.AddTerminal(*name);
|
||||
delete name;
|
||||
return id;
|
||||
|
@ -153,10 +145,6 @@ ParsingDriver::add_model_variable(string *name, string *olag)
|
|||
<< *name
|
||||
<< " has lag " << lag << endl;
|
||||
}
|
||||
|
||||
if ((type == eEndogenous) || (type == eExogenous))
|
||||
mod_file->variable_table.AddVariable(*name, lag);
|
||||
|
||||
NodeID id = mod_file->model_tree.AddTerminal(*name, lag);
|
||||
delete name;
|
||||
delete olag;
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
/*! \file
|
||||
\version 1.0
|
||||
\date 04/09/2004
|
||||
\par This file implements the VariableTable class methodes.
|
||||
*/
|
||||
//------------------------------------------------------------------------------
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#include "VariableTable.hh"
|
||||
|
||||
VariableTable::VariableTable(const SymbolTable &symbol_table_arg,
|
||||
|
@ -16,45 +8,30 @@ VariableTable::VariableTable(const SymbolTable &symbol_table_arg,
|
|||
symbol_table(symbol_table_arg),
|
||||
mod_param(mod_param_arg)
|
||||
{
|
||||
// Empty
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
VariableTable::~VariableTable()
|
||||
int
|
||||
VariableTable::AddVariable(const string &iName, int iLag)
|
||||
{
|
||||
// Empty
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
int VariableTable::AddVariable(string iName, int iLag)
|
||||
{
|
||||
int lVariableID;
|
||||
//Variable lVariable;
|
||||
varKey key;
|
||||
// Testing if symbol exists
|
||||
if (!symbol_table.Exist(iName))
|
||||
{
|
||||
string msg = "unknown symbol: " + iName;
|
||||
(* error) (msg.c_str());
|
||||
cerr << "Unknown symbol: " << iName << endl;
|
||||
exit(-1);
|
||||
}
|
||||
// Testing if variable exists in VaiableTable
|
||||
lVariableID = getID(iName,iLag);
|
||||
// Testing if variable exists in VariableTable
|
||||
int lVariableID = getID(iName,iLag);
|
||||
if (lVariableID != -1)
|
||||
{
|
||||
return lVariableID;
|
||||
}
|
||||
return lVariableID;
|
||||
|
||||
lVariableID = mVariableIndex.size();
|
||||
// Making variable struct and key
|
||||
//lVariable.type = SymbolTable::getType(iName);
|
||||
//lVariable.symbol_id = SymbolTable::getID(iName);
|
||||
//lVariable.variable_id = lVariableID;
|
||||
key = make_pair(iName,iLag);
|
||||
varKey key = make_pair(iName, iLag);
|
||||
// Pushing variable on VariableTable
|
||||
//mVariableTable[key] = lVariable;
|
||||
mVariableTable[key] = lVariableID;
|
||||
mVariableIndex.push_back(key);
|
||||
// Setting variable numbers
|
||||
|
||||
// Setting dynamic variables numbers
|
||||
Type type = getType(lVariableID);
|
||||
if (type == eEndogenous)
|
||||
mod_param.var_endo_nbr++;
|
||||
|
@ -62,7 +39,8 @@ int VariableTable::AddVariable(string iName, int iLag)
|
|||
mod_param.var_exo_nbr++;
|
||||
if (type == eExogenousDet)
|
||||
mod_param.var_exo_det_nbr++;
|
||||
// Setting Maximum and minimum lags
|
||||
|
||||
// Setting maximum and minimum lags
|
||||
if (mod_param.max_lead < iLag)
|
||||
mod_param.max_lead = iLag;
|
||||
else if (-mod_param.max_lag > iLag)
|
||||
|
@ -97,123 +75,56 @@ int VariableTable::AddVariable(string iName, int iLag)
|
|||
default:
|
||||
;
|
||||
}
|
||||
|
||||
return mVariableIndex.size()-1;
|
||||
return lVariableID;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void VariableTable::decSymbolID(string iName, int id, int iLag, Type iType)
|
||||
void
|
||||
VariableTable::Sort()
|
||||
{
|
||||
int lVariableID;
|
||||
Variable lVariable;
|
||||
varKey key;
|
||||
|
||||
// Testing if variable exists in VaiableTable
|
||||
lVariableID = getID(iName,iLag);
|
||||
if (lVariableID == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Making variable struct and key to update
|
||||
lVariable.type = iType;
|
||||
lVariable.symbol_id = id-1;
|
||||
lVariable.variable_id = lVariableID;
|
||||
// Updating VariableTable with new variable
|
||||
key = make_pair(iName,iLag);
|
||||
mVariableTable[key] = lVariableID;
|
||||
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void VariableTable::Sort()
|
||||
{
|
||||
varKey key;
|
||||
int variable;
|
||||
// To store variable lags
|
||||
vector<pair<unsigned long long int,int> > VarToSort;
|
||||
vector<int> IDs;
|
||||
vector<int> Lags;
|
||||
vector<Type> Types;
|
||||
|
||||
// Trivial case where no ordering is necessary
|
||||
if (mVariableIndex.size() == 1)
|
||||
{
|
||||
mSortedVariableID.push_back(0);
|
||||
mPrintFormatIndex.push_back(0);
|
||||
return;
|
||||
}
|
||||
// First putting types into TypesToSort
|
||||
for (unsigned int id=0; id < mVariableIndex.size(); id++)
|
||||
|
||||
/* The type of key for lexicographic ordering over variables:
|
||||
the key is equal to (type, lag, symbol_id) */
|
||||
typedef pair<Type, pair<int, int> > lexicographic_key_type;
|
||||
|
||||
// Construct the vector matching keys to their varIDs
|
||||
vector<pair<lexicographic_key_type, int> > VarToSort;
|
||||
for (unsigned int varID = 0; varID < mVariableIndex.size(); varID++)
|
||||
{
|
||||
key = mVariableIndex[id];
|
||||
variable = mVariableTable[key];
|
||||
//IDs.push_back(variable.symbol_id);
|
||||
//Types.push_back(variable.type);
|
||||
IDs.push_back(getSymbolID(variable));
|
||||
Types.push_back(getType(variable));
|
||||
Lags.push_back(key.second);
|
||||
unsigned long long int lag = Lags[id]+mod_param.max_lag;
|
||||
lag = lag << (4*sizeof(int));
|
||||
unsigned long long int type = Types[id];
|
||||
type = type << 8*sizeof(int);
|
||||
unsigned long long int sort_pound = IDs[id]+lag+type;
|
||||
VarToSort.push_back(make_pair(sort_pound,id));
|
||||
Type type = getType(varID);
|
||||
int symbolID = getSymbolID(varID);
|
||||
int lag = mVariableIndex[varID].second;
|
||||
VarToSort.push_back(make_pair(make_pair(type, make_pair(lag, symbolID)), varID));
|
||||
}
|
||||
// Uncomment this to debug
|
||||
/*
|
||||
cout << "Before sorting\n";
|
||||
cout << "S T L ID pound \n";
|
||||
for (int id=0; id < VarToSort.size(); id++)
|
||||
{
|
||||
Type type = Types[VarToSort[id].second];
|
||||
int lag = Lags[VarToSort[id].second];
|
||||
int ID = IDs[VarToSort[id].second];
|
||||
cout << SymbolTable::getNameByID(type, ID) << " "
|
||||
<< type << " "
|
||||
<< lag << " "
|
||||
<< ID << " "
|
||||
<< VarToSort[id].first << "\n";
|
||||
}
|
||||
*/
|
||||
// Sorting variables
|
||||
|
||||
// Sort variables using the lexicographic ordering
|
||||
sort(VarToSort.begin(), VarToSort.end());
|
||||
// Puting "sorted Ids" into mSortedVariableID
|
||||
|
||||
// Fill mSortedVariableID and mPrintFormatIndex
|
||||
mSortedVariableID.resize(VarToSort.size());
|
||||
mPrintFormatIndex.resize(VarToSort.size());
|
||||
Type type = Types[VarToSort[0].second];
|
||||
Type type = getType(VarToSort[0].second);
|
||||
int index = 0;
|
||||
for (unsigned int id = 0; id < VarToSort.size(); id++)
|
||||
for (unsigned int sortedID = 0; sortedID < VarToSort.size(); sortedID++)
|
||||
{
|
||||
int id2 = VarToSort[id].second;
|
||||
mSortedVariableID[id2] = id;
|
||||
if (type == Types[id2])
|
||||
int varID = VarToSort[sortedID].second;
|
||||
mSortedVariableID[varID] = sortedID;
|
||||
if (type == getType(varID))
|
||||
{
|
||||
mPrintFormatIndex[id2] = index;
|
||||
mPrintFormatIndex[varID] = index;
|
||||
index++;
|
||||
}
|
||||
else
|
||||
{
|
||||
mPrintFormatIndex[id2] = 0;
|
||||
type = Types[id2];
|
||||
mPrintFormatIndex[varID] = 0;
|
||||
type = getType(varID);
|
||||
index = 1;
|
||||
}
|
||||
}
|
||||
// Uncomment this to debug
|
||||
/*
|
||||
cout << "After sorting\n";
|
||||
cout << "S T L ID SVID PIDX\n";
|
||||
for (int id=0; id < VarToSort.size(); id++)
|
||||
{
|
||||
Type type = Types[VarToSort[id].second];
|
||||
int lag = Lags[VarToSort[id].second];
|
||||
int ID = IDs[VarToSort[id].second];
|
||||
cout << SymbolTable::getNameByID(Types[id], IDs[id]) << " "
|
||||
<< Types[id] << " "
|
||||
<< Lags[id] << " "
|
||||
<< IDs[id] << " "
|
||||
<< mSortedVariableID[id] << " "
|
||||
<< mPrintFormatIndex[id] << "\n";
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
|
|
@ -34,8 +34,8 @@ class DataTree
|
|||
protected :
|
||||
//! A reference to the symbol table
|
||||
SymbolTable &symbol_table;
|
||||
//! A reference to the variable table
|
||||
VariableTable &variable_table;
|
||||
//! The variable table
|
||||
VariableTable variable_table;
|
||||
|
||||
/*! A list of structures "token" */
|
||||
TreeList mModelTree;
|
||||
|
@ -123,7 +123,7 @@ public :
|
|||
inline NodeID AddAssign(NodeID iArg1, NodeID iArg2);
|
||||
public :
|
||||
/*! Constructor */
|
||||
DataTree(SymbolTable &symbol_table_arg, VariableTable &variable_table_arg);
|
||||
DataTree(SymbolTable &symbol_table_arg, ModelParameters &mod_param_arg);
|
||||
/*! Destructor */
|
||||
~DataTree();
|
||||
};
|
||||
|
@ -232,15 +232,7 @@ inline NodeID DataTree::AddTerminal(std::string iArgName, int iLag)
|
|||
type == eExogenousDet ||
|
||||
type == eExogenous ||
|
||||
type == eRecursiveVariable)
|
||||
{
|
||||
id = variable_table.getID(iArgName,iLag);
|
||||
if (id == -1)
|
||||
{
|
||||
std::string msg = "unknown variable " + iArgName;
|
||||
(* error) (msg.c_str());
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
id = variable_table.AddVariable(iArgName,iLag);
|
||||
else
|
||||
id = symbol_table.getID(iArgName);
|
||||
|
||||
|
|
|
@ -22,8 +22,6 @@ public:
|
|||
ModelParameters model_parameters;
|
||||
//! Symbol table
|
||||
SymbolTable symbol_table;
|
||||
//! Variable table
|
||||
VariableTable variable_table;
|
||||
//! Numerical constants table
|
||||
NumericalConstants num_constants;
|
||||
//! Model equations and their derivatives
|
||||
|
|
|
@ -89,7 +89,7 @@ private :
|
|||
|
||||
public:
|
||||
//! Constructor
|
||||
ModelTree(SymbolTable &symbol_table_arg, VariableTable &variable_table_arg, ModelParameters &mod_param_arg, const NumericalConstants &num_constants);
|
||||
ModelTree(SymbolTable &symbol_table_arg, ModelParameters &mod_param_arg, const NumericalConstants &num_constants);
|
||||
//! Destructor
|
||||
~ModelTree();
|
||||
//! When Jacobian (vs endogenous) is written this flag is set to true
|
||||
|
|
|
@ -1,134 +1,106 @@
|
|||
#ifndef _VARIABLETABLE_HH
|
||||
#define _VARIABLETABLE_HH
|
||||
//------------------------------------------------------------------------------
|
||||
/** \file
|
||||
* \version 1.0
|
||||
* \date 12/16/2003
|
||||
* \par This file defines the VariableTable class .
|
||||
*/
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#include "SymbolTable.hh"
|
||||
//------------------------------------------------------------------------------
|
||||
/*!
|
||||
\class Variable
|
||||
\brief Variable struct
|
||||
*/
|
||||
struct Variable
|
||||
{
|
||||
/*! Variable type */
|
||||
Type type;
|
||||
/*! Symbol ID */
|
||||
int symbol_id;
|
||||
/*! Variable ID */
|
||||
int variable_id;
|
||||
};
|
||||
/*! Variable key type to acced variable table elements */
|
||||
typedef std::pair<std::string, int> varKey;
|
||||
//------------------------------------------------------------------------------
|
||||
/*!
|
||||
\class VariableTable
|
||||
\brief This class is used to store variables as they appear
|
||||
in the model (with their lead or lag)
|
||||
*/
|
||||
|
||||
//! This class is used to store variables as they appear in the model (with their lead or lag)
|
||||
/*! \todo Raise exceptions when requesting ordered IDs before calling to Sort() */
|
||||
class VariableTable
|
||||
{
|
||||
private :
|
||||
private:
|
||||
//! A reference to the symbol table
|
||||
const SymbolTable &symbol_table;
|
||||
//! A reference to model parameters
|
||||
ModelParameters &mod_param;
|
||||
/*! Variable table data */
|
||||
std::map<varKey,int> mVariableTable;
|
||||
/*! Index (IDs) of variables in variable table */
|
||||
std::vector<varKey> mVariableIndex;
|
||||
/*! Variable IDs of sorted variable table */
|
||||
std::vector<int> mSortedVariableID;
|
||||
/*! Output index for variable table */
|
||||
std::vector<int> mPrintFormatIndex;
|
||||
public :
|
||||
/*! */
|
||||
VariableTable(const SymbolTable &symbol_table_arg, ModelParameters &mod_param_arg);
|
||||
/*! */
|
||||
~VariableTable();
|
||||
/*! Find type and ID in SymbolTable
|
||||
- Increment variable_id;
|
||||
- Make variable
|
||||
- Push variable on variabletable
|
||||
//! Variable key type to acced variable table elements
|
||||
typedef pair<string, int> varKey;
|
||||
//! Maps a pair (symbol, lag) to an ID
|
||||
map<varKey, int> mVariableTable;
|
||||
//! Maps an ID to a pair (symbol, lag)
|
||||
/*! It is the reverse map of mVariableTable */
|
||||
vector<varKey> mVariableIndex;
|
||||
//! Variable IDs of sorted variable table
|
||||
vector<int> mSortedVariableID;
|
||||
//! For each variable, gives its index number among variables of the same type
|
||||
/*! It is the index used in the output file:
|
||||
- in the lead/lag matrix
|
||||
- in the right hand side of equations (such as y(index))
|
||||
*/
|
||||
int AddVariable(std::string iName, int iLag);
|
||||
/*! Pointer to error function of parser class */
|
||||
void (* error) (const char* m);
|
||||
/*! Decremente a symbol id of a variable */
|
||||
void decSymbolID(std::string iName, int id, int iLag, Type iType);
|
||||
/*! Return VariableTable[name,lag].variable_id */
|
||||
inline int getID(std::string iName, int iLag);
|
||||
/*! Return lag of variable */
|
||||
inline int getLag(int iID);
|
||||
/*! Return symbol ID of variable */
|
||||
inline int getSymbolID(int ivarID);
|
||||
/*! Gets varibale type */
|
||||
inline Type getType(int ivarID);
|
||||
/*! Gets nomber of variables in mVariableTable */
|
||||
inline int size();
|
||||
/*! Gets variable ID of sorted variable table */
|
||||
inline int getSortID(int);
|
||||
/*! Return variable index to print in format : y(index) or oo_.y_simul(index) ... */
|
||||
inline int getPrintIndex(int iVarID);
|
||||
/*! Sorts variable table */
|
||||
vector<int> mPrintFormatIndex;
|
||||
public:
|
||||
VariableTable(const SymbolTable &symbol_table_arg, ModelParameters &mod_param_arg);
|
||||
//! Adds a variable in the table, and returns its (newly allocated) varID
|
||||
/*! Also works if the variable already exists */
|
||||
int AddVariable(const string &iName, int iLag);
|
||||
//! Return variable ID
|
||||
inline int getID(const string &iName, int iLag) const;
|
||||
//! Return lag of variable
|
||||
inline int getLag(int ivarID) const;
|
||||
//! Return symbol ID of variable
|
||||
inline int getSymbolID(int ivarID) const;
|
||||
//! Get variable type
|
||||
inline Type getType(int ivarID) const;
|
||||
//! Get number of variables in mVariableTable
|
||||
inline int size() const;
|
||||
//! Get variable ID of sorted variable table
|
||||
inline int getSortID(int iVarID) const;
|
||||
//! Return variable index to print in format : y(index) or oo_.y_simul(index) ...
|
||||
inline int getPrintIndex(int iVarID) const;
|
||||
//! Sorts variable table
|
||||
/*! The order used is a lexicographic order over the tuple (type, lag, symbolID) */
|
||||
void Sort();
|
||||
};
|
||||
inline int VariableTable::getSortID(int iVarID)
|
||||
|
||||
inline int
|
||||
VariableTable::getSortID(int iVarID) const
|
||||
{
|
||||
return mSortedVariableID[iVarID];
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
inline int VariableTable::getPrintIndex(int iVarID)
|
||||
inline int
|
||||
VariableTable::getPrintIndex(int iVarID) const
|
||||
{
|
||||
return mPrintFormatIndex[iVarID];
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
inline int VariableTable::getID(std::string iName, int iLag)
|
||||
inline int
|
||||
VariableTable::getID(const string &iName, int iLag) const
|
||||
{
|
||||
if (mVariableTable.find(make_pair(iName, iLag)) == mVariableTable.end())
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
map<varKey, int>::const_iterator it = mVariableTable.find(make_pair(iName, iLag));
|
||||
if (it == mVariableTable.end())
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
return mVariableTable[make_pair(iName, iLag)];
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
inline Type VariableTable::getType(int ivarID)
|
||||
inline Type
|
||||
VariableTable::getType(int ivarID) const
|
||||
{
|
||||
varKey key = mVariableIndex[ivarID];
|
||||
//return mVariableTable[key].type;
|
||||
return symbol_table.getType(key.first);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
inline int VariableTable::getSymbolID(int ivarID)
|
||||
inline int
|
||||
VariableTable::getSymbolID(int ivarID) const
|
||||
{
|
||||
varKey key = mVariableIndex[ivarID];
|
||||
//return mVariableTable[key].symbol_id;
|
||||
return symbol_table.getID(key.first);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
inline int VariableTable::getLag(int iID)
|
||||
inline int
|
||||
VariableTable::getLag(int ivarID) const
|
||||
{
|
||||
return mVariableIndex[iID].second;
|
||||
return mVariableIndex[ivarID].second;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
inline int VariableTable::size()
|
||||
inline int
|
||||
VariableTable::size() const
|
||||
{
|
||||
return mVariableTable.size();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue