From ef640070c27305584eef65fdbc5de7168f706e82 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 7 Apr 2015 15:11:39 +0200
Subject: [PATCH] preprocessor: move reindexing functions from DynamicModel to
 ModelTree

---
 DynamicModel.cc | 57 -------------------------------------------
 DynamicModel.hh |  9 -------
 ModFile.cc      |  2 +-
 ModelTree.cc    | 64 +++++++++++++++++++++++++++++++++++++++++++++++++
 ModelTree.hh    |  9 ++++++-
 5 files changed, 73 insertions(+), 68 deletions(-)

diff --git a/DynamicModel.cc b/DynamicModel.cc
index f27b1ca3..e881b302 100644
--- a/DynamicModel.cc
+++ b/DynamicModel.cc
@@ -3429,47 +3429,6 @@ DynamicModel::writeDynamicFile(const string &basename, bool block, bool bytecode
     writeDynamicMFile(t_basename);
 }
 
-void
-DynamicModel::reindexTrendSymbolsMap(DynamicModel &dynamic_model, SymbolTable &orig_symbol_table)
-{
-  map<int, expr_t> orig_trend_symbols_map = trend_symbols_map;
-  trend_symbols_map.clear();
-  for (map<int, expr_t>::const_iterator it = orig_trend_symbols_map.begin();
-       it != orig_trend_symbols_map.end(); it++)
-    try
-      {
-        vector<int> symb_id (1, symbol_table.getID(orig_symbol_table.getName(it->first)));
-        addTrendVariables(symb_id,
-                          it->second->cloneDynamicReindex(dynamic_model, orig_symbol_table));
-      }
-    catch(...)
-      {
-        cerr << "Error: unused exo in trend symbols." << endl;
-        exit(EXIT_FAILURE);
-      }
-}
-
-void
-DynamicModel::reindexNonstationarySymbolsMap(DynamicModel &dynamic_model, SymbolTable &orig_symbol_table)
-{
-  nonstationary_symbols_map_t orig_nonstationary_symbols_map = nonstationary_symbols_map;
-  nonstationary_symbols_map.clear();
-  for (nonstationary_symbols_map_t::const_iterator it = orig_nonstationary_symbols_map.begin();
-       it != orig_nonstationary_symbols_map.end(); it++)
-    try
-      {
-        vector<int> symb_id (1, symbol_table.getID(orig_symbol_table.getName(it->first)));
-        addNonstationaryVariables(symb_id,
-                                  it->second.first,
-                                  it->second.second->cloneDynamicReindex(dynamic_model, orig_symbol_table));
-      }
-  catch(...)
-      {
-        cerr << "Error: unused exo in nonstationary symbols." << endl;
-        exit(EXIT_FAILURE);
-      }
-}
-
 void
 DynamicModel::resetDataTree()
 {
@@ -3483,22 +3442,6 @@ DynamicModel::resetDataTree()
   second_deriv_external_function_node_map.clear();
 }
 
-void
-DynamicModel::reindexEquations(DynamicModel &dynamic_model, SymbolTable &orig_symbol_table)
-{
-  map<int, expr_t> orig_local_variables_table = local_variables_table;
-  local_variables_table.clear();
-  for (map<int, expr_t>::const_iterator it = orig_local_variables_table.begin();
-       it != orig_local_variables_table.end(); it++)
-    dynamic_model.AddLocalVariable(symbol_table.getID(orig_symbol_table.getName(it->first)),
-                                   it->second->cloneDynamicReindex(dynamic_model, orig_symbol_table));
-
-  vector<BinaryOpNode *>eqbak = equations;
-  equations.clear();
-  for (size_t i = 0; i < eqbak.size(); i++)
-    dynamic_model.addEquation(eqbak[i]->cloneDynamicReindex(dynamic_model, orig_symbol_table), equations_lineno[i]);
-}
-
 void
 DynamicModel::cloneDynamic(DynamicModel &dynamic_model) const
 {
diff --git a/DynamicModel.hh b/DynamicModel.hh
index 79f085de..e6a56675 100644
--- a/DynamicModel.hh
+++ b/DynamicModel.hh
@@ -235,15 +235,6 @@ public:
   /*! It assumes that the dynamic model given in argument has just been allocated */
   void cloneDynamic(DynamicModel &dynamic_model) const;
 
-  //! reindex equations after change in symbol_table
-  void reindexEquations(DynamicModel &dynamic_model, SymbolTable &orig_symbol_table);
-
-  //! reindex trend_symbol_map after change in symbol_table
-  void reindexTrendSymbolsMap(DynamicModel &dynamic_model, SymbolTable &orig_symbol_table);
-
-  //! reindex nonstationary_symbol_map after change in symbol_table
-  void reindexNonstationarySymbolsMap(DynamicModel &dynamic_model, SymbolTable &orig_symbol_table);
-
   //! reset DataTree vars
   void resetDataTree();
 
diff --git a/ModFile.cc b/ModFile.cc
index 570ad72a..d9f36dbb 100644
--- a/ModFile.cc
+++ b/ModFile.cc
@@ -314,7 +314,7 @@ ModFile::transformPass(bool nostrict)
       SymbolTable orig_symbol_table = symbol_table;
       symbol_table.rmExo(unusedExo);
       dynamic_model.resetDataTree();
-      dynamic_model.reindexEquations(dynamic_model, orig_symbol_table);
+      dynamic_model.reindex(orig_symbol_table);
       vector<Statement *> orig_statements = statements;
       statements.clear();
       for (vector<Statement *>::iterator it = orig_statements.begin(); it != orig_statements.end(); it++)
diff --git a/ModelTree.cc b/ModelTree.cc
index 688d4f29..b95a508e 100644
--- a/ModelTree.cc
+++ b/ModelTree.cc
@@ -1413,6 +1413,70 @@ ModelTree::addAuxEquation(expr_t eq)
   aux_equations.push_back(beq);
 }
 
+void
+ModelTree::reindex(SymbolTable &orig_symbol_table)
+{
+  reindexEquations(orig_symbol_table);
+  reindexTrendSymbolsMap(orig_symbol_table);
+  reindexNonstationarySymbolsMap(orig_symbol_table);
+}
+
+void
+ModelTree::reindexEquations(SymbolTable &orig_symbol_table)
+{
+  map<int, expr_t> orig_local_variables_table = local_variables_table;
+  local_variables_table.clear();
+  for (map<int, expr_t>::const_iterator it = orig_local_variables_table.begin();
+       it != orig_local_variables_table.end(); it++)
+    AddLocalVariable(symbol_table.getID(orig_symbol_table.getName(it->first)),
+                     it->second->cloneDynamicReindex(*this, orig_symbol_table));
+
+  vector<BinaryOpNode *>eqbak = equations;
+  equations.clear();
+  for (size_t i = 0; i < eqbak.size(); i++)
+    addEquation(eqbak[i]->cloneDynamicReindex(*this, orig_symbol_table), equations_lineno[i]);
+}
+
+void
+ModelTree::reindexTrendSymbolsMap(SymbolTable &orig_symbol_table)
+{
+  map<int, expr_t> orig_trend_symbols_map = trend_symbols_map;
+  trend_symbols_map.clear();
+  for (map<int, expr_t>::const_iterator it = orig_trend_symbols_map.begin();
+       it != orig_trend_symbols_map.end(); it++)
+    try
+      {
+        vector<int> symb_id (1, symbol_table.getID(orig_symbol_table.getName(it->first)));
+        addTrendVariables(symb_id, it->second->cloneDynamicReindex(*this, orig_symbol_table));
+      }
+    catch(...)
+      {
+        cerr << "Error: unused exo in trend symbols." << endl;
+        exit(EXIT_FAILURE);
+      }
+}
+
+void
+ModelTree::reindexNonstationarySymbolsMap(SymbolTable &orig_symbol_table)
+{
+  nonstationary_symbols_map_t orig_nonstationary_symbols_map = nonstationary_symbols_map;
+  nonstationary_symbols_map.clear();
+  for (nonstationary_symbols_map_t::const_iterator it = orig_nonstationary_symbols_map.begin();
+       it != orig_nonstationary_symbols_map.end(); it++)
+    try
+      {
+        vector<int> symb_id (1, symbol_table.getID(orig_symbol_table.getName(it->first)));
+        addNonstationaryVariables(symb_id,
+                                  it->second.first,
+                                  it->second.second->cloneDynamicReindex(*this, orig_symbol_table));
+      }
+  catch(...)
+      {
+        cerr << "Error: unused exo in nonstationary symbols." << endl;
+        exit(EXIT_FAILURE);
+      }
+}
+
 void
 ModelTree::addTrendVariables(vector<int> trend_vars, expr_t growth_factor) throw (TrendException)
 {
diff --git a/ModelTree.hh b/ModelTree.hh
index 4d161daf..b86376b2 100644
--- a/ModelTree.hh
+++ b/ModelTree.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2014 Dynare Team
+ * Copyright (C) 2003-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -303,6 +303,13 @@ public:
   void addEquation(expr_t eq, int lineno, vector<pair<string, string> > &eq_tags);
   //! Declare a node as an auxiliary equation of the model, adding it at the end of the list of auxiliary equations
   void addAuxEquation(expr_t eq);
+  void reindex(SymbolTable &orig_symbol_table);
+  //! reindex equations after change in symbol_table
+  void reindexEquations(SymbolTable &orig_symbol_table);
+  //! reindex trend_symbol_map after change in symbol_table
+  void reindexTrendSymbolsMap(SymbolTable &orig_symbol_table);
+  //! reindex nonstationary_symbol_map after change in symbol_table
+  void reindexNonstationarySymbolsMap(SymbolTable &orig_symbol_table);
   //! Returns the number of equations in the model
   int equation_number() const;
   //! Adds a trend variable with its growth factor
-- 
GitLab