From 0b0d4d04551522d0b21480851f26f1eaa6539ea4 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan.bastani@ens.fr>
Date: Thu, 29 Mar 2012 16:45:46 +0200
Subject: [PATCH] estimation: rewrite options output

---
 ComputingTasks.cc | 115 +++++++++++++---------------------------------
 ComputingTasks.hh |   7 +--
 Statement.cc      |   3 --
 Statement.hh      |   6 ---
 4 files changed, 34 insertions(+), 97 deletions(-)

diff --git a/ComputingTasks.cc b/ComputingTasks.cc
index d564589a..3660946e 100644
--- a/ComputingTasks.cc
+++ b/ComputingTasks.cc
@@ -1882,9 +1882,9 @@ CorrPriorStatement::writeOutput(ostream &output, const string &basename) const
   string lhs_field;
   get_base_name(symbol_table.getType(name), lhs_field);
 
-  output << "eifind = get_new_or_existing_ei_index('" << lhs_field << "_prior_index', '"
+  output << "eifind = get_new_or_existing_ei_index('" << lhs_field << "_corr_prior_index', '"
          << name << "', '" << name1 << "');" << endl
-         << "estimation_info." << lhs_field << "_prior_index(eifind) = {'"
+         << "estimation_info." << lhs_field << "_corr_prior_index(eifind) = {'"
          << name << "_" << name1 << "'};" << endl;
 
   lhs_field = "estimation_info." + lhs_field + "_corr(eifind)";
@@ -1903,8 +1903,7 @@ BasicOptionsStatement::BasicOptionsStatement(const string &name_arg,
                                              const OptionsList &options_list_arg) :
   name(name_arg),
   subsample_name(subsample_name_arg),
-  options_list(options_list_arg),
-  first_statement_encountered(false)
+  options_list(options_list_arg)
 {
 }
 
@@ -1913,15 +1912,6 @@ BasicOptionsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConso
 {
 }
 
-void
-BasicOptionsStatement::writeOptionsIndex(ostream &output, const string &lhs_field) const
-{
-  if (first_statement_encountered)
-    output << "options_indx = 1;" << endl;
-  else
-    output << "options_indx = size(estimation_info" << lhs_field << "_index, 2) + 1;" << endl;
-}
-
 void
 BasicOptionsStatement::get_base_name(const SymbolType symb_type, string &lhs_field) const
 {
@@ -1944,32 +1934,22 @@ BasicOptionsStatement::writeCommonOutputHelper(ostream &output, const string &fi
 {
   OptionsList::num_options_t::const_iterator itn = options_list.num_options.find(field);
   if (itn != options_list.num_options.end())
-    output << "estimation_info" << lhs_field << "(options_indx)." << field
-           << " = " << itn->second << ";" << endl;
-
-  OptionsList::date_options_t::const_iterator itd = options_list.date_options.find(field);
-  if (itd != options_list.date_options.end())
-    output << "estimation_info" << lhs_field << "(options_indx)." << field
-           << " = '" << itd->second << "';" << endl;
-}
-
-void
-BasicOptionsStatement::writeSubsampleName(ostream &output) const
-{
-  if (!subsample_name.empty())
-    output << ":" << subsample_name;
+    output <<  lhs_field << "." << field << " = " << itn->second << ";" << endl;
 }
 
 void
-BasicOptionsStatement::writeSubsampleInfo(ostream &output, const string &lhs_field, const string name1, const string name2) const
+BasicOptionsStatement::writeOptionsOutput(ostream &output, string &lhs_field, const string &name2) const
 {
   if (subsample_name.empty())
-    return;
-
-  output << "subsamples_indx = get_existing_subsamples_indx('" << name1 << "', '" << name2 << "');" << endl;
-  output << "range_indx = get_subsamples_range_indx(subsamples_indx, '" << subsample_name << "');" << endl;
-  output << "estimation_info" << lhs_field << "(options_indx).date1 = estimation_info.subsamples(subsamples_indx).range(range_indx).date1;" << endl;
-  output << "estimation_info" << lhs_field << "(options_indx).date2 = estimation_info.subsamples(subsamples_indx).range(range_indx).date2;" << endl;
+    lhs_field += ".options(1)";
+  else
+    {
+      output << "subsamples_indx = get_existing_subsamples_indx('" << name << "','" << name2 << "');" << endl
+             << lhs_field << ".range_index = estimation_info.subsamples(subsamples_indx).range_index;" << endl
+             << "eisind = get_subsamples_range_indx(subsamples_indx, '" << subsample_name << "');" << endl;
+      lhs_field += ".subsample_options(eisind)";
+    }
+  writeCommonOutput(output, lhs_field);
 }
 
 OptionsStatement::OptionsStatement(const string &name_arg,
@@ -1979,28 +1959,14 @@ OptionsStatement::OptionsStatement(const string &name_arg,
 {
 }
 
-void
-OptionsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
-{
-  BasicOptionsStatement::checkPass(mod_file_struct, warnings);
-  if (!mod_file_struct.options_statement_present)
-    first_statement_encountered = true;
-  mod_file_struct.options_statement_present = true;
-}
-
 void
 OptionsStatement::writeOutput(ostream &output, const string &basename) const
 {
-  string lhs_field = ".parameters.options";
-
-  writeOptionsIndex(output, lhs_field);
-  output << "estimation_info" << lhs_field <<"_index(options_indx) = {'" << name;
-  writeSubsampleName(output);
-  output << "'};" << endl
-         << "estimation_info" << lhs_field << "(options_indx).name = '" << name << "';" << endl;
-
-  writeCommonOutput(output, lhs_field);
-  writeSubsampleInfo(output, lhs_field, name, "");
+  string lhs_field = "estimation_info.parameters(eifind)";
+  output << "eifind = get_new_or_existing_ei_index('parameter_options_index', '"
+         << name << "', '');" << endl
+         << "estimation_info.parameter_options_index(eifind) = {'" << name << "'};" << endl;
+  writeOptionsOutput(output, lhs_field, "");
 }
 
 StdOptionsStatement::StdOptionsStatement(const string &name_arg,
@@ -2012,30 +1978,17 @@ StdOptionsStatement::StdOptionsStatement(const string &name_arg,
 {
 }
 
-void
-StdOptionsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
-{
-  BasicOptionsStatement::checkPass(mod_file_struct, warnings);
-  if (!mod_file_struct.std_options_statement_present)
-    first_statement_encountered = true;
-  mod_file_struct.std_options_statement_present = true;
-}
-
 void
 StdOptionsStatement::writeOutput(ostream &output, const string &basename) const
 {
   string lhs_field;
   get_base_name(symbol_table.getType(name), lhs_field);
-  lhs_field = "." + lhs_field + ".options";
-
-  writeOptionsIndex(output, lhs_field);
-  output << "estimation_info" << lhs_field << "_index(options_indx) = {'" << name;
-  writeSubsampleName(output);
-  output << "'};" << endl
-         << "estimation_info" << lhs_field << "(options_indx).name = '" << name << "';" << endl;
+  output << "eifind = get_new_or_existing_ei_index('" << lhs_field << "_options_index', '"
+         << name << "', '');" << endl
+         << "estimation_info." << lhs_field << "_options_index(eifind) = {'" << name << "'};" << endl;
 
-  writeCommonOutput(output, lhs_field);
-  writeSubsampleInfo(output, lhs_field, name, "");
+  lhs_field = "estimation_info." + lhs_field + "(eifind)";
+  writeOptionsOutput(output, lhs_field, "");
 }
 
 CorrOptionsStatement::CorrOptionsStatement(const string &name_arg1, const string &name_arg2,
@@ -2058,8 +2011,6 @@ CorrOptionsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsol
            << "types." << endl;
       exit(EXIT_FAILURE);
     }
-  if (!mod_file_struct.corr_options_statement_present)
-    first_statement_encountered = true;
 }
 
 void
@@ -2067,15 +2018,15 @@ CorrOptionsStatement::writeOutput(ostream &output, const string &basename) const
 {
   string lhs_field;
   get_base_name(symbol_table.getType(name), lhs_field);
-  lhs_field = "." + lhs_field + "_corr.options";
 
-  writeOptionsIndex(output, lhs_field);
-  output << "estimation_info" << lhs_field << "_index(options_indx) = {'" << name << "_" << name1;
-  writeSubsampleName(output);
-  output << "'};" << endl
-         << "estimation_info" << lhs_field << "(options_indx).name1 = '" << name << "';" << endl
-         << "estimation_info" << lhs_field << "(options_indx).name2 = '" << name1 << "';" << endl;
+  output << "eifind = get_new_or_existing_ei_index('" << lhs_field << "_corr_options_index', '"
+         << name << "', '" << name1 << "');" << endl
+         << "estimation_info." << lhs_field << "_corr_options_index(eifind) = {'"
+         << name << "_" << name1 << "'};" << endl;
 
-  writeCommonOutput(output, lhs_field);
-  writeSubsampleInfo(output, lhs_field, name, name1);
+  lhs_field = "estimation_info." + lhs_field + "_corr(eifind)";
+  writeOptionsOutput(output, lhs_field, name1);
+
+  output << lhs_field << ".name1 = '" << name << "';" << endl
+         << lhs_field << ".name2 = '" << name1 << "';" << endl;
 }
diff --git a/ComputingTasks.hh b/ComputingTasks.hh
index 26f6359d..494b0469 100644
--- a/ComputingTasks.hh
+++ b/ComputingTasks.hh
@@ -684,24 +684,20 @@ protected:
   const string name;
   const string subsample_name;
   const OptionsList options_list;
-  bool first_statement_encountered;
   BasicOptionsStatement(const string &name_arg,
                         const string &subsample_name_arg,
                         const OptionsList &options_list_arg);
   void get_base_name(const SymbolType symb_type, string &lhs_field) const;
   virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings);
-  void writeOptionsIndex(ostream &output, const string &lhs_field) const;
+  void writeOptionsOutput(ostream &output, string &lhs_field, const string &name2) const;
   void writeCommonOutput(ostream &output, const string &lhs_field) const;
   void writeCommonOutputHelper(ostream &output, const string &field, const string &lhs_field) const;
-  void writeSubsampleName(ostream &output) const;
-  void writeSubsampleInfo(ostream &output, const string &lhs_field, const string name1, const string name2) const;
 };
 
 class OptionsStatement : public BasicOptionsStatement
 {
 public:
   OptionsStatement(const string &name_arg, const string &subsample_name_arg, const OptionsList &options_list_arg);
-  virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings);
   virtual void writeOutput(ostream &output, const string &basename) const;
 };
 
@@ -714,7 +710,6 @@ public:
                       const string &subsample_name_arg,
                       const OptionsList &options_list_arg,
                       const SymbolTable &symbol_table_arg);
-  virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings);
   virtual void writeOutput(ostream &output, const string &basename) const;
 };
 
diff --git a/Statement.cc b/Statement.cc
index 8bdb38ed..7ca25265 100644
--- a/Statement.cc
+++ b/Statement.cc
@@ -45,9 +45,6 @@ ModFileStructure::ModFileStructure() :
   dsge_var_estimated(false),
   bayesian_irf_present(false),
   estimation_data_statement_present(false),
-  options_statement_present(false),
-  std_options_statement_present(false),
-  corr_options_statement_present(false),
   last_markov_switching_chain(0)
 {
 }
diff --git a/Statement.hh b/Statement.hh
index 33cc44d8..92b1ff19 100644
--- a/Statement.hh
+++ b/Statement.hh
@@ -89,12 +89,6 @@ public:
   bool bayesian_irf_present;
   //! Whether there is a data statement present
   bool estimation_data_statement_present;
-  //! Whether there is a options statement present
-  bool options_statement_present;
-  //! Whether there is a std options statement present
-  bool std_options_statement_present;
-  //! Whether there is a corr options statement present
-  bool corr_options_statement_present;
   //! Last chain number for Markov Switching statement
   int last_markov_switching_chain;
 };
-- 
GitLab