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-bf33cf982152
time-shift
sebastien 2006-12-14 22:19:39 +00:00
parent 4f9e8a6e27
commit be18e12a43
9 changed files with 116 additions and 255 deletions

View File

@ -17,9 +17,9 @@ using namespace std;
#include "NumericalConstants.hh" #include "NumericalConstants.hh"
#include "DataTree.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), symbol_table(symbol_table_arg),
variable_table(variable_table_arg), variable_table(symbol_table_arg, mod_param_arg),
NoOpCode(-1), NullID(NULL) NoOpCode(-1), NullID(NULL)
{ {
offset = 1; offset = 1;

View File

@ -2,8 +2,7 @@
#include "Interface.hh" #include "Interface.hh"
ModFile::ModFile() : symbol_table(model_parameters), ModFile::ModFile() : symbol_table(model_parameters),
variable_table(symbol_table, model_parameters), model_tree(symbol_table, model_parameters, num_constants),
model_tree(symbol_table, variable_table, model_parameters, num_constants),
order(-1), linear(-1) order(-1), linear(-1)
{ {
} }

View File

@ -34,9 +34,10 @@ inline NodeID MetaToken::getDerivativeAddress(int iVarID, const ModelTree &model
return iter->second; return iter->second;
} }
ModelTree::ModelTree(SymbolTable &symbol_table_arg, VariableTable &variable_table_arg, ModelTree::ModelTree(SymbolTable &symbol_table_arg,
ModelParameters &mod_param_arg, const NumericalConstants &num_constants_arg) : ModelParameters &mod_param_arg,
DataTree(symbol_table_arg, variable_table_arg), const NumericalConstants &num_constants_arg) :
DataTree(symbol_table_arg, mod_param_arg),
mod_param(mod_param_arg), mod_param(mod_param_arg),
num_constants(num_constants_arg), num_constants(num_constants_arg),
computeHessian(false), computeHessian(false),

View File

@ -44,7 +44,6 @@ ParsingDriver::parse(const string &f)
mod_file->model_tree.error = error; mod_file->model_tree.error = error;
mod_file->symbol_table.error = error; mod_file->symbol_table.error = error;
mod_file->variable_table.error = error;
expression.setNumericalConstants(&mod_file->num_constants); expression.setNumericalConstants(&mod_file->num_constants);
tmp_symbol_table = new TmpSymbolTable(mod_file->symbol_table); tmp_symbol_table = new TmpSymbolTable(mod_file->symbol_table);
@ -128,13 +127,6 @@ NodeID
ParsingDriver::add_model_variable(string *name) ParsingDriver::add_model_variable(string *name)
{ {
check_symbol_existence(*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); NodeID id = mod_file->model_tree.AddTerminal(*name);
delete name; delete name;
return id; return id;
@ -153,10 +145,6 @@ ParsingDriver::add_model_variable(string *name, string *olag)
<< *name << *name
<< " has lag " << lag << endl; << " 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); NodeID id = mod_file->model_tree.AddTerminal(*name, lag);
delete name; delete name;
delete olag; delete olag;

View File

@ -1,14 +1,6 @@
/*! \file
\version 1.0
\date 04/09/2004
\par This file implements the VariableTable class methodes.
*/
//------------------------------------------------------------------------------
#include <iostream> #include <iostream>
#include <vector>
#include <algorithm> #include <algorithm>
using namespace std;
//------------------------------------------------------------------------------
#include "VariableTable.hh" #include "VariableTable.hh"
VariableTable::VariableTable(const SymbolTable &symbol_table_arg, VariableTable::VariableTable(const SymbolTable &symbol_table_arg,
@ -16,45 +8,30 @@ VariableTable::VariableTable(const SymbolTable &symbol_table_arg,
symbol_table(symbol_table_arg), symbol_table(symbol_table_arg),
mod_param(mod_param_arg) mod_param(mod_param_arg)
{ {
// Empty
} }
//------------------------------------------------------------------------------ int
VariableTable::~VariableTable() 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 // Testing if symbol exists
if (!symbol_table.Exist(iName)) if (!symbol_table.Exist(iName))
{ {
string msg = "unknown symbol: " + iName; cerr << "Unknown symbol: " << iName << endl;
(* error) (msg.c_str());
exit(-1); exit(-1);
} }
// Testing if variable exists in VaiableTable // Testing if variable exists in VariableTable
lVariableID = getID(iName,iLag); int lVariableID = getID(iName,iLag);
if (lVariableID != -1) if (lVariableID != -1)
{
return lVariableID; return lVariableID;
}
lVariableID = mVariableIndex.size(); lVariableID = mVariableIndex.size();
// Making variable struct and key // Making variable struct and key
//lVariable.type = SymbolTable::getType(iName); varKey key = make_pair(iName, iLag);
//lVariable.symbol_id = SymbolTable::getID(iName);
//lVariable.variable_id = lVariableID;
key = make_pair(iName,iLag);
// Pushing variable on VariableTable // Pushing variable on VariableTable
//mVariableTable[key] = lVariable;
mVariableTable[key] = lVariableID; mVariableTable[key] = lVariableID;
mVariableIndex.push_back(key); mVariableIndex.push_back(key);
// Setting variable numbers
// Setting dynamic variables numbers
Type type = getType(lVariableID); Type type = getType(lVariableID);
if (type == eEndogenous) if (type == eEndogenous)
mod_param.var_endo_nbr++; mod_param.var_endo_nbr++;
@ -62,7 +39,8 @@ int VariableTable::AddVariable(string iName, int iLag)
mod_param.var_exo_nbr++; mod_param.var_exo_nbr++;
if (type == eExogenousDet) if (type == eExogenousDet)
mod_param.var_exo_det_nbr++; mod_param.var_exo_det_nbr++;
// Setting Maximum and minimum lags
// Setting maximum and minimum lags
if (mod_param.max_lead < iLag) if (mod_param.max_lead < iLag)
mod_param.max_lead = iLag; mod_param.max_lead = iLag;
else if (-mod_param.max_lag > iLag) else if (-mod_param.max_lag > iLag)
@ -97,123 +75,56 @@ int VariableTable::AddVariable(string iName, int iLag)
default: default:
; ;
} }
return lVariableID;
return mVariableIndex.size()-1;
} }
//------------------------------------------------------------------------------ void
void VariableTable::decSymbolID(string iName, int id, int iLag, Type iType) VariableTable::Sort()
{ {
int lVariableID; // Trivial case where no ordering is necessary
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;
if (mVariableIndex.size() == 1) if (mVariableIndex.size() == 1)
{ {
mSortedVariableID.push_back(0); mSortedVariableID.push_back(0);
mPrintFormatIndex.push_back(0); mPrintFormatIndex.push_back(0);
return; 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]; Type type = getType(varID);
variable = mVariableTable[key]; int symbolID = getSymbolID(varID);
//IDs.push_back(variable.symbol_id); int lag = mVariableIndex[varID].second;
//Types.push_back(variable.type); VarToSort.push_back(make_pair(make_pair(type, make_pair(lag, symbolID)), varID));
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));
} }
// Uncomment this to debug
/* // Sort variables using the lexicographic ordering
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(VarToSort.begin(), VarToSort.end()); sort(VarToSort.begin(), VarToSort.end());
// Puting "sorted Ids" into mSortedVariableID
// Fill mSortedVariableID and mPrintFormatIndex
mSortedVariableID.resize(VarToSort.size()); mSortedVariableID.resize(VarToSort.size());
mPrintFormatIndex.resize(VarToSort.size()); mPrintFormatIndex.resize(VarToSort.size());
Type type = Types[VarToSort[0].second]; Type type = getType(VarToSort[0].second);
int index = 0; 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; int varID = VarToSort[sortedID].second;
mSortedVariableID[id2] = id; mSortedVariableID[varID] = sortedID;
if (type == Types[id2]) if (type == getType(varID))
{ {
mPrintFormatIndex[id2] = index; mPrintFormatIndex[varID] = index;
index++; index++;
} }
else else
{ {
mPrintFormatIndex[id2] = 0; mPrintFormatIndex[varID] = 0;
type = Types[id2]; type = getType(varID);
index = 1; 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";
} }
*/
}
//------------------------------------------------------------------------------

View File

@ -34,8 +34,8 @@ class DataTree
protected : protected :
//! A reference to the symbol table //! A reference to the symbol table
SymbolTable &symbol_table; SymbolTable &symbol_table;
//! A reference to the variable table //! The variable table
VariableTable &variable_table; VariableTable variable_table;
/*! A list of structures "token" */ /*! A list of structures "token" */
TreeList mModelTree; TreeList mModelTree;
@ -123,7 +123,7 @@ public :
inline NodeID AddAssign(NodeID iArg1, NodeID iArg2); inline NodeID AddAssign(NodeID iArg1, NodeID iArg2);
public : public :
/*! Constructor */ /*! Constructor */
DataTree(SymbolTable &symbol_table_arg, VariableTable &variable_table_arg); DataTree(SymbolTable &symbol_table_arg, ModelParameters &mod_param_arg);
/*! Destructor */ /*! Destructor */
~DataTree(); ~DataTree();
}; };
@ -232,15 +232,7 @@ inline NodeID DataTree::AddTerminal(std::string iArgName, int iLag)
type == eExogenousDet || type == eExogenousDet ||
type == eExogenous || type == eExogenous ||
type == eRecursiveVariable) type == eRecursiveVariable)
{ id = variable_table.AddVariable(iArgName,iLag);
id = variable_table.getID(iArgName,iLag);
if (id == -1)
{
std::string msg = "unknown variable " + iArgName;
(* error) (msg.c_str());
exit(-1);
}
}
else else
id = symbol_table.getID(iArgName); id = symbol_table.getID(iArgName);

View File

@ -22,8 +22,6 @@ public:
ModelParameters model_parameters; ModelParameters model_parameters;
//! Symbol table //! Symbol table
SymbolTable symbol_table; SymbolTable symbol_table;
//! Variable table
VariableTable variable_table;
//! Numerical constants table //! Numerical constants table
NumericalConstants num_constants; NumericalConstants num_constants;
//! Model equations and their derivatives //! Model equations and their derivatives

View File

@ -89,7 +89,7 @@ private :
public: public:
//! Constructor //! 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 //! Destructor
~ModelTree(); ~ModelTree();
//! When Jacobian (vs endogenous) is written this flag is set to true //! When Jacobian (vs endogenous) is written this flag is set to true

View File

@ -1,39 +1,16 @@
#ifndef _VARIABLETABLE_HH #ifndef _VARIABLETABLE_HH
#define _VARIABLETABLE_HH #define _VARIABLETABLE_HH
//------------------------------------------------------------------------------
/** \file using namespace std;
* \version 1.0
* \date 12/16/2003
* \par This file defines the VariableTable class .
*/
//------------------------------------------------------------------------------
#include <map> #include <map>
#include <string> #include <string>
#include <vector> #include <vector>
//------------------------------------------------------------------------------
#include "SymbolTable.hh" #include "SymbolTable.hh"
//------------------------------------------------------------------------------
/*! //! This class is used to store variables as they appear in the model (with their lead or lag)
\class Variable /*! \todo Raise exceptions when requesting ordered IDs before calling to Sort() */
\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)
*/
class VariableTable class VariableTable
{ {
private: private:
@ -41,94 +18,89 @@ private :
const SymbolTable &symbol_table; const SymbolTable &symbol_table;
//! A reference to model parameters //! A reference to model parameters
ModelParameters &mod_param; ModelParameters &mod_param;
/*! Variable table data */ //! Variable key type to acced variable table elements
std::map<varKey,int> mVariableTable; typedef pair<string, int> varKey;
/*! Index (IDs) of variables in variable table */ //! Maps a pair (symbol, lag) to an ID
std::vector<varKey> mVariableIndex; map<varKey, int> mVariableTable;
/*! Variable IDs of sorted variable table */ //! Maps an ID to a pair (symbol, lag)
std::vector<int> mSortedVariableID; /*! It is the reverse map of mVariableTable */
/*! Output index for variable table */ vector<varKey> mVariableIndex;
std::vector<int> mPrintFormatIndex; //! Variable IDs of sorted variable table
public : vector<int> mSortedVariableID;
/*! */ //! For each variable, gives its index number among variables of the same type
VariableTable(const SymbolTable &symbol_table_arg, ModelParameters &mod_param_arg); /*! It is the index used in the output file:
/*! */ - in the lead/lag matrix
~VariableTable(); - in the right hand side of equations (such as y(index))
/*! Find type and ID in SymbolTable
- Increment variable_id;
- Make variable
- Push variable on variabletable
*/ */
int AddVariable(std::string iName, int iLag); vector<int> mPrintFormatIndex;
/*! Pointer to error function of parser class */ public:
void (* error) (const char* m); VariableTable(const SymbolTable &symbol_table_arg, ModelParameters &mod_param_arg);
/*! Decremente a symbol id of a variable */ //! Adds a variable in the table, and returns its (newly allocated) varID
void decSymbolID(std::string iName, int id, int iLag, Type iType); /*! Also works if the variable already exists */
/*! Return VariableTable[name,lag].variable_id */ int AddVariable(const string &iName, int iLag);
inline int getID(std::string iName, int iLag); //! Return variable ID
/*! Return lag of variable */ inline int getID(const string &iName, int iLag) const;
inline int getLag(int iID); //! Return lag of variable
/*! Return symbol ID of variable */ inline int getLag(int ivarID) const;
inline int getSymbolID(int ivarID); //! Return symbol ID of variable
/*! Gets varibale type */ inline int getSymbolID(int ivarID) const;
inline Type getType(int ivarID); //! Get variable type
/*! Gets nomber of variables in mVariableTable */ inline Type getType(int ivarID) const;
inline int size(); //! Get number of variables in mVariableTable
/*! Gets variable ID of sorted variable table */ inline int size() const;
inline int getSortID(int); //! Get variable ID of sorted variable table
/*! Return variable index to print in format : y(index) or oo_.y_simul(index) ... */ inline int getSortID(int iVarID) const;
inline int getPrintIndex(int iVarID); //! Return variable index to print in format : y(index) or oo_.y_simul(index) ...
/*! Sorts variable table */ inline int getPrintIndex(int iVarID) const;
//! Sorts variable table
/*! The order used is a lexicographic order over the tuple (type, lag, symbolID) */
void Sort(); void Sort();
}; };
inline int VariableTable::getSortID(int iVarID)
inline int
VariableTable::getSortID(int iVarID) const
{ {
return mSortedVariableID[iVarID]; return mSortedVariableID[iVarID];
} }
//------------------------------------------------------------------------------ inline int
inline int VariableTable::getPrintIndex(int iVarID) VariableTable::getPrintIndex(int iVarID) const
{ {
return mPrintFormatIndex[iVarID]; return mPrintFormatIndex[iVarID];
} }
//------------------------------------------------------------------------------ inline int
inline int VariableTable::getID(std::string iName, int iLag) VariableTable::getID(const string &iName, int iLag) const
{
if (mVariableTable.find(make_pair(iName, iLag)) == mVariableTable.end())
{ {
map<varKey, int>::const_iterator it = mVariableTable.find(make_pair(iName, iLag));
if (it == mVariableTable.end())
return -1; return -1;
}
else else
{ return it->second;
return mVariableTable[make_pair(iName, iLag)];
}
} }
//------------------------------------------------------------------------------ inline Type
inline Type VariableTable::getType(int ivarID) VariableTable::getType(int ivarID) const
{ {
varKey key = mVariableIndex[ivarID]; varKey key = mVariableIndex[ivarID];
//return mVariableTable[key].type;
return symbol_table.getType(key.first); return symbol_table.getType(key.first);
} }
//------------------------------------------------------------------------------ inline int
inline int VariableTable::getSymbolID(int ivarID) VariableTable::getSymbolID(int ivarID) const
{ {
varKey key = mVariableIndex[ivarID]; varKey key = mVariableIndex[ivarID];
//return mVariableTable[key].symbol_id;
return symbol_table.getID(key.first); return symbol_table.getID(key.first);
} }
//------------------------------------------------------------------------------ inline int
inline int VariableTable::getLag(int iID) VariableTable::getLag(int ivarID) const
{ {
return mVariableIndex[iID].second; return mVariableIndex[ivarID].second;
} }
//------------------------------------------------------------------------------ inline int
inline int VariableTable::size() VariableTable::size() const
{ {
return mVariableTable.size(); return mVariableTable.size();
} }