VariableTable.cc 6.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/*! \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"
//------------------------------------------------------------------------------
14
15
16
17
map<varKey,int> VariableTable::mVariableTable;
vector<varKey> VariableTable::mVariableIndex;
vector<int> VariableTable::mSortedVariableID;
vector<int> VariableTable::mPrintFormatIndex;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void (* VariableTable::error) (const char* ) = NULL;
//------------------------------------------------------------------------------
VariableTable::VariableTable()
{
  // Empty
}

//------------------------------------------------------------------------------
VariableTable::~VariableTable()
{
  // Empty
}

//------------------------------------------------------------------------------
int VariableTable::AddVariable(string iName, int iLag)
{
  int     lVariableID;
35
  //Variable  lVariable;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  varKey      key;
  // Testing if symbol exists
  if (!SymbolTable::Exist(iName))
    {
      string msg = "unknown symbol: " + iName;
      (* error) (msg.c_str());
      exit(-1);
    }
  // Testing if variable exists in VaiableTable
  lVariableID = getID(iName,iLag);
  if (lVariableID != -1)
    {
      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);
  // Pushing variable on VariableTable
  //mVariableTable[key] = lVariable;
  mVariableTable[key] = lVariableID;
  mVariableIndex.push_back(key);
  // Setting lags field in symbol table
  SymbolTable::AddLag(iName, iLag);
  // Setting variable numbers
  Type type = getType(lVariableID);
64
65
66
67
68
69
  if (type == eEndogenous)
    ModelParameters::var_endo_nbr++;
  if (type == eExogenous)
    ModelParameters::var_exo_nbr++;
  if (type == eExogenousDet)
    ModelParameters::var_exo_det_nbr++;
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  // Setting Maximum and minimum lags
  if (ModelParameters::max_lead < iLag)
    {
      ModelParameters::max_lead = iLag;
    }
  else if (-ModelParameters::max_lag > iLag)
    {
      ModelParameters::max_lag = -iLag;
    }
  switch(type)
    {
    case eEndogenous:
      if (ModelParameters::max_endo_lead < iLag)
        {
          ModelParameters::max_endo_lead = iLag;
        }
      else if (-ModelParameters::max_endo_lag > iLag)
        {
          ModelParameters::max_endo_lag = -iLag;
        }
      break;
    case eExogenous:
      if (ModelParameters::max_exo_lead < iLag)
        {
          ModelParameters::max_exo_lead = iLag;
        }
      else if (-ModelParameters::max_exo_lag > iLag)
        {
          ModelParameters::max_exo_lag = -iLag;
        }
      break;
    case eExogenousDet:
      if (ModelParameters::max_exo_det_lead < iLag)
        {
          ModelParameters::max_exo_det_lead = iLag;
        }
      else if (-ModelParameters::max_exo_det_lag > iLag)
        {
          ModelParameters::max_exo_det_lag = -iLag;
        }
      break;
    case eRecursiveVariable:
      if (ModelParameters::max_recur_lead < iLag)
        {
          ModelParameters::max_recur_lead = iLag;
        }
      else if (-ModelParameters::max_recur_lag > iLag)
        {
          ModelParameters::max_recur_lag = -iLag;
        }
      break;
    default:
      ;
    }

  return mVariableIndex.size()-1;
}

//------------------------------------------------------------------------------
void VariableTable::decSymbolID(string iName, int id, int iLag, Type iType)
{
  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;

}

//------------------------------------------------------------------------------
152
void VariableTable::Sort(void)
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
{
  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)
    {
      mSortedVariableID.push_back(0);
      mPrintFormatIndex.push_back(0);
      return;
    }
  // First putting types into TypesToSort
  for (unsigned int id=0; id < mVariableIndex.size(); id++)
    {
      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]+ModelParameters::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
  /*
    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());
  // Puting "sorted Ids" into mSortedVariableID
  mSortedVariableID.resize(VarToSort.size());
  mPrintFormatIndex.resize(VarToSort.size());
  Type type = Types[VarToSort[0].second];
  int index = 0;
  for (unsigned int id = 0; id < VarToSort.size(); id++)
    {
      int id2 = VarToSort[id].second;
      mSortedVariableID[id2] = id;
      if (type == Types[id2])
        {
          mPrintFormatIndex[id2] = index;
          index++;
        }
      else
        {
          mPrintFormatIndex[id2] = 0;
          type = Types[id2];
          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";
    }
  */
}

//------------------------------------------------------------------------------