preprocessor/VariableTable.cc

116 lines
3.6 KiB
C++

/*
* Copyright (C) 2003-2009 Dynare Team
*
* This file is part of Dynare.
*
* Dynare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dynare is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <cstdlib>
#include "VariableTable.hh"
VariableTable::VariableTable(const SymbolTable &symbol_table_arg) :
symbol_table(symbol_table_arg),
var_endo_nbr(0), var_exo_nbr(0), var_exo_det_nbr(0),
max_lag(0), max_lead(0),
max_endo_lag(0), max_endo_lead(0),
max_exo_lag(0), max_exo_lead(0),
max_exo_det_lag(0), max_exo_det_lead(0)
{
}
int
VariableTable::addVariable(int symb_id, int lag) throw (DynJacobianColsAlreadyComputedException)
{
if (dyn_jacobian_cols_table.size() != 0)
throw DynJacobianColsAlreadyComputedException();
var_key_type key = make_pair(lag, symb_id);
// Testing if variable already exists
variable_table_type::const_iterator it = variable_table.find(key);
if (it != variable_table.end())
return it->second;
int var_id = size();
variable_table[key] = var_id;
inv_variable_table.push_back(key);
// Setting maximum and minimum lags
if (max_lead < lag)
max_lead = lag;
else if (-max_lag > lag)
max_lag = -lag;
switch(symbol_table.getType(symb_id))
{
case eEndogenous:
var_endo_nbr++;
if (max_endo_lead < lag)
max_endo_lead = lag;
else if (-max_endo_lag > lag)
max_endo_lag = -lag;
break;
case eExogenous:
var_exo_nbr++;
if (max_exo_lead < lag)
max_exo_lead = lag;
else if (-max_exo_lag > lag)
max_exo_lag = -lag;
break;
case eExogenousDet:
var_exo_det_nbr++;
if (max_exo_det_lead < lag)
max_exo_det_lead = lag;
else if (-max_exo_det_lag > lag)
max_exo_det_lag = -lag;
break;
default:
cerr << "VariableTable::addVariable(): forbidden variable type" << endl;
exit(EXIT_FAILURE);
}
return var_id;
}
void
VariableTable::computeDynJacobianCols() throw (DynJacobianColsAlreadyComputedException)
{
if (dyn_jacobian_cols_table.size() != 0)
throw DynJacobianColsAlreadyComputedException();
dyn_jacobian_cols_table.resize(size());
variable_table_type::const_iterator it;
// Assign the first columns to endogenous, using the lexicographic order over (lag, symbol_id) implemented in variable_table map
int sorted_id = 0;
for(it = variable_table.begin(); it != variable_table.end(); it++)
{
if (symbol_table.getType(it->first.second) == eEndogenous)
dyn_jacobian_cols_table[it->second] = sorted_id++;
}
// Assign subsequent columns to exogenous and then exogenous deterministic, using an offset + symbol_type_specific_id
for(it = variable_table.begin(); it != variable_table.end(); it++)
{
if (symbol_table.getType(it->first.second) == eExogenous)
dyn_jacobian_cols_table[it->second] = var_endo_nbr + symbol_table.getTypeSpecificID(it->first.second);
if (symbol_table.getType(it->first.second) == eExogenousDet)
dyn_jacobian_cols_table[it->second] = var_endo_nbr + symbol_table.exo_nbr() + symbol_table.getTypeSpecificID(it->first.second);
}
}