From 14948401e490d54a6d5b34720bac0500b90352dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org> Date: Wed, 18 Jul 2018 16:52:12 +0200 Subject: [PATCH] C++11: convert PriorDistributions to a class enum --- src/CodeInterpreter.hh | 24 ++++++------- src/ComputingTasks.cc | 76 +++++++++++++++++++++--------------------- src/ComputingTasks.hh | 2 +- src/DynareBison.yy | 48 +++++++++++++------------- src/ParsingDriver.cc | 8 ++--- 5 files changed, 79 insertions(+), 79 deletions(-) diff --git a/src/CodeInterpreter.hh b/src/CodeInterpreter.hh index ef8cfdc0..28fec2d5 100644 --- a/src/CodeInterpreter.hh +++ b/src/CodeInterpreter.hh @@ -240,18 +240,18 @@ enum external_function_type ExternalFunctionSecondDerivative }; -enum PriorDistributions - { - eNoShape = 0, - eBeta = 1, - eGamma = 2, - eNormal = 3, - eInvGamma = 4, - eInvGamma1 = 4, - eUniform = 5, - eInvGamma2 = 6, - eDirichlet = 7, - eWeibull = 8 +enum class PriorDistributions + { + noShape = 0, + beta = 1, + gamma = 2, + normal = 3, + invGamma = 4, + invGamma1 = 4, + uniform = 5, + invGamma2 = 6, + dirichlet = 7, + weibull = 8 }; enum class NodeTreeReference diff --git a/src/ComputingTasks.cc b/src/ComputingTasks.cc index 64c29275..63ad33e1 100644 --- a/src/ComputingTasks.cc +++ b/src/ComputingTasks.cc @@ -1459,7 +1459,7 @@ EstimatedParamsStatement::checkPass(ModFileStructure &mod_file_struct, WarningCo mod_file_struct.dsge_prior_weight_in_estimated_params = true; // Handle case of degenerate beta prior - if (it.prior == eBeta) + if (it.prior == PriorDistributions::beta) try { if (it.mean->eval(eval_context_t()) == 0.5 @@ -1553,7 +1553,7 @@ EstimatedParamsStatement::writeOutput(ostream &output, const string &basename, b output << ", "; it.up_bound->writeOutput(output); output << ", " - << it.prior << ", "; + << static_cast<int>(it.prior) << ", "; it.mean->writeOutput(output); output << ", "; it.std->writeOutput(output); @@ -1598,7 +1598,7 @@ EstimatedParamsStatement::writeJsonOutput(ostream &output) const output << "\", \"upper_bound\": \""; it->up_bound->writeJsonOutput(output, {}, {}); output << "\", \"prior_distribution\": " - << it->prior + << static_cast<int>(it->prior) << ", \"mean\": \""; it->mean->writeJsonOutput(output, {}, {}); output << "\", \"std\": \""; @@ -3711,7 +3711,7 @@ JointPriorStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsoli exit(EXIT_FAILURE); } - if (prior_shape == eNoShape) + if (prior_shape == PriorDistributions::noShape) { cerr << "ERROR: You must pass the shape option to the prior statement." << endl; exit(EXIT_FAILURE); @@ -3760,8 +3760,8 @@ JointPriorStatement::writeOutput(ostream &output, const string &basename, bool m writeOutputHelper(output, "median", lhs_field); writeOutputHelper(output, "mode", lhs_field); - assert(prior_shape != eNoShape); - output << lhs_field << ".shape = " << prior_shape << ";" << endl; + assert(prior_shape != PriorDistributions::noShape); + output << lhs_field << ".shape = " << static_cast<int>(prior_shape) << ";" << endl; writeOutputHelper(output, "shift", lhs_field); writeOutputHelper(output, "stdev", lhs_field); @@ -3821,31 +3821,31 @@ JointPriorStatement::writeJsonOutput(ostream &output) const output << ", \"shape\": "; switch (prior_shape) { - case eBeta: + case PriorDistributions::beta: output << "\"beta\""; break; - case eGamma: + case PriorDistributions::gamma: output << "\"gamma\""; break; - case eNormal: + case PriorDistributions::normal: output << "\"normal\""; break; - case eInvGamma: + case PriorDistributions::invGamma: output << "\"inv_gamma\""; break; - case eUniform: + case PriorDistributions::uniform: output << "\"uniform\""; break; - case eInvGamma2: + case PriorDistributions::invGamma2: output << "\"inv_gamma2\""; break; - case eDirichlet: + case PriorDistributions::dirichlet: output << "\"dirichlet\""; break; - case eWeibull: + case PriorDistributions::weibull: output << "\"weibull\""; break; - case eNoShape: + case PriorDistributions::noShape: cerr << "Impossible case." << endl; exit(EXIT_FAILURE); } @@ -3871,7 +3871,7 @@ BasicPriorStatement::BasicPriorStatement(string name_arg, void BasicPriorStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) { - if (prior_shape == eNoShape) + if (prior_shape == PriorDistributions::noShape) { cerr << "ERROR: You must pass the shape option to the prior statement." << endl; exit(EXIT_FAILURE); @@ -3934,8 +3934,8 @@ BasicPriorStatement::writeCommonOutput(ostream &output, const string &lhs_field) writeCommonOutputHelper(output, "median", lhs_field); writeCommonOutputHelper(output, "mode", lhs_field); - assert(prior_shape != eNoShape); - output << lhs_field << ".shape = " << prior_shape << ";" << endl; + assert(prior_shape != PriorDistributions::noShape); + output << lhs_field << ".shape = " << static_cast<int>(prior_shape) << ";" << endl; writeCommonOutputHelper(output, "shift", lhs_field); writeCommonOutputHelper(output, "stdev", lhs_field); @@ -4034,32 +4034,32 @@ BasicPriorStatement::writeCShape(ostream &output) const output << "shape = "; switch (prior_shape) { - case eBeta: + case PriorDistributions::beta: output << "\"beta\";" << endl; break; - case eGamma: + case PriorDistributions::gamma: output << "\"gamma\";" << endl; break; - case eNormal: + case PriorDistributions::normal: output << "\"normal\";" << endl; break; - case eInvGamma: + case PriorDistributions::invGamma: output << "\"inv_gamma\";" << endl; break; - case eUniform: + case PriorDistributions::uniform: output << "\"uniform\";" << endl; break; - case eInvGamma2: + case PriorDistributions::invGamma2: output << "\"inv_gamma2\";" << endl; break; - case eDirichlet: + case PriorDistributions::dirichlet: output << "\"dirichlet\";" << endl; break; - case eWeibull: + case PriorDistributions::weibull: output << "\"weibull\";" << endl; break; - case eNoShape: - assert(prior_shape != eNoShape); + case PriorDistributions::noShape: + assert(prior_shape != PriorDistributions::noShape); } } @@ -4069,32 +4069,32 @@ BasicPriorStatement::writeJsonShape(ostream &output) const output << "\"shape\": "; switch (prior_shape) { - case eBeta: + case PriorDistributions::beta: output << "\"beta\""; break; - case eGamma: + case PriorDistributions::gamma: output << "\"gamma\""; break; - case eNormal: + case PriorDistributions::normal: output << "\"normal\""; break; - case eInvGamma: + case PriorDistributions::invGamma: output << "\"inv_gamma\""; break; - case eUniform: + case PriorDistributions::uniform: output << "\"uniform\""; break; - case eInvGamma2: + case PriorDistributions::invGamma2: output << "\"inv_gamma2\""; break; - case eDirichlet: + case PriorDistributions::dirichlet: output << "\"dirichlet\""; break; - case eWeibull: + case PriorDistributions::weibull: output << "\"weibull\""; break; - case eNoShape: - assert(prior_shape != eNoShape); + case PriorDistributions::noShape: + assert(prior_shape != PriorDistributions::noShape); } } diff --git a/src/ComputingTasks.hh b/src/ComputingTasks.hh index 3a98e09c..2e696da6 100644 --- a/src/ComputingTasks.hh +++ b/src/ComputingTasks.hh @@ -464,7 +464,7 @@ public: type = 0; name = ""; name2 = ""; - prior = eNoShape; + prior = PriorDistributions::noShape; init_val = datatree.NaN; low_bound = datatree.MinusInfinity; up_bound = datatree.Infinity; diff --git a/src/DynareBison.yy b/src/DynareBison.yy index ff910d4e..8c01230c 100644 --- a/src/DynareBison.yy +++ b/src/DynareBison.yy @@ -1697,41 +1697,41 @@ osr_bounds_elem : symbol COMMA expression COMMA expression ';' ; prior_distribution : BETA - { $$ = eBeta; } + { $$ = PriorDistributions::beta; } | GAMMA - { $$ = eGamma; } + { $$ = PriorDistributions::gamma; } | NORMAL - { $$ = eNormal; } + { $$ = PriorDistributions::normal; } | INV_GAMMA - { $$ = eInvGamma; } + { $$ = PriorDistributions::invGamma; } | INV_GAMMA1 - { $$ = eInvGamma1; } + { $$ = PriorDistributions::invGamma1; } | UNIFORM - { $$ = eUniform; } + { $$ = PriorDistributions::uniform; } | INV_GAMMA2 - { $$ = eInvGamma2; } + { $$ = PriorDistributions::invGamma2; } | DIRICHLET - { $$ = eDirichlet; } + { $$ = PriorDistributions::dirichlet; } | WEIBULL - { $$ = eWeibull; } + { $$ = PriorDistributions::weibull; } ; prior_pdf : BETA_PDF - { $$ = eBeta; } + { $$ = PriorDistributions::beta; } | GAMMA_PDF - { $$ = eGamma; } + { $$ = PriorDistributions::gamma; } | NORMAL_PDF - { $$ = eNormal; } + { $$ = PriorDistributions::normal; } | INV_GAMMA_PDF - { $$ = eInvGamma; } + { $$ = PriorDistributions::invGamma; } | INV_GAMMA1_PDF - { $$ = eInvGamma1; } + { $$ = PriorDistributions::invGamma1; } | UNIFORM_PDF - { $$ = eUniform; } + { $$ = PriorDistributions::uniform; } | INV_GAMMA2_PDF - { $$ = eInvGamma2; } + { $$ = PriorDistributions::invGamma2; } | WEIBULL_PDF - { $$ = eWeibull; } + { $$ = PriorDistributions::weibull; } ; date_str : DATES { $$ = $1; } @@ -1799,19 +1799,19 @@ subsamples_name_list : subsamples_name_list COMMA o_subsample_name | o_subsample_name ; -prior : symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';' +prior : symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = PriorDistributions::noShape; } '(' prior_options_list ')' ';' { driver.set_prior($1, new string ("")); } - | symbol '.' symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';' + | symbol '.' symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = PriorDistributions::noShape; } '(' prior_options_list ')' ';' { driver.set_prior($1, $3); } - | SYMBOL_VEC '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' joint_prior_options_list ')' ';' + | SYMBOL_VEC '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = PriorDistributions::noShape; } '(' joint_prior_options_list ')' ';' { driver.set_joint_prior($1); } - | STD '(' symbol ')' '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';' + | STD '(' symbol ')' '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = PriorDistributions::noShape; } '(' prior_options_list ')' ';' { driver.set_std_prior($3, new string ("")); } - | STD '(' symbol ')' '.' symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';' + | STD '(' symbol ')' '.' symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = PriorDistributions::noShape; } '(' prior_options_list ')' ';' { driver.set_std_prior($3, $6); } - | CORR '(' symbol COMMA symbol ')' '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';' + | CORR '(' symbol COMMA symbol ')' '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = PriorDistributions::noShape; } '(' prior_options_list ')' ';' { driver.set_corr_prior($3, $5, new string ("")); } - | CORR '(' symbol COMMA symbol ')' '.' symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';' + | CORR '(' symbol COMMA symbol ')' '.' symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = PriorDistributions::noShape; } '(' prior_options_list ')' ';' { driver.set_corr_prior($3, $5, $8); } ; diff --git a/src/ParsingDriver.cc b/src/ParsingDriver.cc index bdac0815..1192428e 100644 --- a/src/ParsingDriver.cc +++ b/src/ParsingDriver.cc @@ -1892,7 +1892,7 @@ ParsingDriver::set_prior(string *name, string *subsample_name) mod_file->addStatement(new PriorStatement(*name, *subsample_name, prior_shape, prior_variance, options_list)); options_list.clear(); set_prior_variance(); - prior_shape = eNoShape; + prior_shape = PriorDistributions::noShape; delete name; delete subsample_name; } @@ -1905,7 +1905,7 @@ ParsingDriver::set_joint_prior(vector<string *> *symbol_vec) mod_file->addStatement(new JointPriorStatement(joint_parameters, prior_shape, options_list)); joint_parameters.clear(); options_list.clear(); - prior_shape = eNoShape; + prior_shape = PriorDistributions::noShape; delete symbol_vec; } @@ -2045,7 +2045,7 @@ ParsingDriver::set_std_prior(string *name, string *subsample_name) options_list, mod_file->symbol_table)); options_list.clear(); set_prior_variance(); - prior_shape = eNoShape; + prior_shape = PriorDistributions::noShape; delete name; delete subsample_name; } @@ -2071,7 +2071,7 @@ ParsingDriver::set_corr_prior(string *name1, string *name2, string *subsample_na options_list, mod_file->symbol_table)); options_list.clear(); set_prior_variance(); - prior_shape = eNoShape; + prior_shape = PriorDistributions::noShape; delete name1; delete name2; delete subsample_name; -- GitLab