From f3aa5ce07acce28bbcd910b063c11c70412d32b1 Mon Sep 17 00:00:00 2001 From: "George G. Vega Yon" Date: Tue, 26 Sep 2023 16:10:51 -0600 Subject: [PATCH] Using reserve whenever possible --- barry.hpp | 43 +++++++++++-------- defm.hpp | 11 ++--- include/barry/counters-meat.hpp | 14 +++--- include/barry/model-meat.hpp | 19 ++++---- include/barry/models/defm/defm-meat.hpp | 11 ++--- .../models/geese/geese-meat-likelihood.hpp | 5 ++- .../barry/models/geese/geese-meat-predict.hpp | 25 ++++++++--- include/barry/rules-meat.hpp | 10 +++-- 8 files changed, 84 insertions(+), 54 deletions(-) diff --git a/barry.hpp b/barry.hpp index fc9e79e05..1cb3ef644 100644 --- a/barry.hpp +++ b/barry.hpp @@ -5394,9 +5394,10 @@ COUNTERS_TEMPLATE(void, add_counter)( COUNTERS_TEMPLATE(std::vector, get_names)() const { - std::vector< std::string > out(this->size()); - for (size_t i = 0u; i < out.size(); ++i) - out[i] = this->data.at(i).get_name(); + std::vector< std::string > out; + out.reserve(this->size()); + for (size_t i = 0u; i < this->size(); ++i) + out.push_back(this->data.at(i).get_name()); return out; @@ -5405,9 +5406,10 @@ COUNTERS_TEMPLATE(std::vector, get_names)() const COUNTERS_TEMPLATE(std::vector, get_descriptions)() const { - std::vector< std::string > out(this->size()); - for (size_t i = 0u; i < out.size(); ++i) - out[i] = data.at(i).get_description(); + std::vector< std::string > out; + out.reserve(this->size()); + for (size_t i = 0u; i < this->size(); ++i) + out.push_back(data.at(i).get_description()); return out; @@ -8230,9 +8232,10 @@ MODEL_TEMPLATE(double, likelihood)( if (support_fun.get_rules_dyn()->size() > 0u) { - std::vector< double > tmp_target(nterms(), 0.0); + std::vector< double > tmp_target; + tmp_target.reserve(nterms()); for (size_t t = 0u; t < nterms(); ++t) - tmp_target[t] = *(target_ + t); + tmp_target.push_back(*(target_ + t)); if (!support_fun.eval_rules_dyn(tmp_target, 0u, 0u)) { @@ -8769,7 +8772,8 @@ MODEL_TEMPLATE(Array_Type, sample)( } else { probs.resize(pset_arrays[a].size()); - std::vector< double > temp_stats(params.size()); + std::vector< double > temp_stats; + temp_stats.reserve(params.size()); const std::vector< double > & stats = pset_stats[a]; int i_matches = -1; @@ -8778,7 +8782,7 @@ MODEL_TEMPLATE(Array_Type, sample)( // Filling out the parameters for (auto p = 0u; p < params.size(); ++p) - temp_stats[p] = stats[array * k + p]; + temp_stats.push_back(stats[array * k + p]); probs[array] = this->likelihood(params, temp_stats, i, false); cumprob += probs[array]; @@ -8825,9 +8829,10 @@ MODEL_TEMPLATE(double, conditional_prob)( A.insert_cell(i, j, A.default_val(), true, false); // Computing the change stats_target - std::vector< double > tmp_counts(counters->size()); - for (size_t ii = 0u; ii < tmp_counts.size(); ++ii) - tmp_counts[ii] = counters->operator[](ii).count(A, i, j); + std::vector< double > tmp_counts; + tmp_counts.reserve(counters->size()); + for (size_t ii = 0u; ii < counters->size(); ++ii) + tmp_counts.push_back(counters->operator[](ii).count(A, i, j)); // If there is a transformation function, it needs to be // applied before dealing with the likelihood. @@ -8951,7 +8956,7 @@ MODEL_TEMPLATE(void, set_transform_model)( // Applying it to the support for (auto s = 0u; s < pset_arrays.size(); ++s) { - std::vector< double > new_stats(0u); + std::vector< double > new_stats; for (auto a = 0u; a < pset_arrays[s].size(); ++a) { @@ -9335,9 +9340,10 @@ template inline std::vector Rules::get_names() const { - std::vector< std::string > out(this->size()); + std::vector< std::string > out; + out.reserve(this->size()); for (size_t i = 0u; i < out.size(); ++i) - out[i] = this->data.at(i).get_name(); + out.push_back(this->data.at(i).get_name()); return out; @@ -9347,9 +9353,10 @@ template inline std::vector Rules::get_descriptions() const { - std::vector< std::string > out(this->size()); + std::vector< std::string > out; + out.reserve(this->size()); for (size_t i = 0u; i < out.size(); ++i) - out[i] = data.at(i).get_description(); + out.push_back(data.at(i).get_description()); return out; diff --git a/defm.hpp b/defm.hpp index 43262becb..624544945 100644 --- a/defm.hpp +++ b/defm.hpp @@ -1337,19 +1337,20 @@ inline std::vector< double > keygen_defm( size_t nrow = Array_.nrow(); size_t ncol = Array_.ncol(); - std::vector< double > res( + std::vector< double > res; + res.reserve( 2u + // Rows + cols ncol * (nrow - 1u) // Markov cells ); - res[0u] = static_cast(nrow); - res[1u] = static_cast(ncol); + res.push_back(static_cast(nrow)); + res.push_back(static_cast(ncol)); - size_t iter = 2u; + // size_t iter = 2u; // Adding the cells for (size_t i = 0u; i < (nrow - 1); ++i) for (size_t j = 0u; j < ncol; ++j) - res[iter++] = Array_(i, j); + res.push_back(Array_(i, j)); return res; diff --git a/include/barry/counters-meat.hpp b/include/barry/counters-meat.hpp index 6c5d13506..d00976b34 100644 --- a/include/barry/counters-meat.hpp +++ b/include/barry/counters-meat.hpp @@ -201,9 +201,10 @@ COUNTERS_TEMPLATE(void, add_counter)( COUNTERS_TEMPLATE(std::vector, get_names)() const { - std::vector< std::string > out(this->size()); - for (size_t i = 0u; i < out.size(); ++i) - out[i] = this->data.at(i).get_name(); + std::vector< std::string > out; + out.reserve(this->size()); + for (size_t i = 0u; i < this->size(); ++i) + out.push_back(this->data.at(i).get_name()); return out; @@ -212,9 +213,10 @@ COUNTERS_TEMPLATE(std::vector, get_names)() const COUNTERS_TEMPLATE(std::vector, get_descriptions)() const { - std::vector< std::string > out(this->size()); - for (size_t i = 0u; i < out.size(); ++i) - out[i] = data.at(i).get_description(); + std::vector< std::string > out; + out.reserve(this->size()); + for (size_t i = 0u; i < this->size(); ++i) + out.push_back(data.at(i).get_description()); return out; diff --git a/include/barry/model-meat.hpp b/include/barry/model-meat.hpp index 623dad566..5b3adb3df 100644 --- a/include/barry/model-meat.hpp +++ b/include/barry/model-meat.hpp @@ -769,9 +769,10 @@ MODEL_TEMPLATE(double, likelihood)( if (support_fun.get_rules_dyn()->size() > 0u) { - std::vector< double > tmp_target(nterms(), 0.0); + std::vector< double > tmp_target; + tmp_target.reserve(nterms()); for (size_t t = 0u; t < nterms(); ++t) - tmp_target[t] = *(target_ + t); + tmp_target.push_back(*(target_ + t)); if (!support_fun.eval_rules_dyn(tmp_target, 0u, 0u)) { @@ -1308,7 +1309,8 @@ MODEL_TEMPLATE(Array_Type, sample)( } else { probs.resize(pset_arrays[a].size()); - std::vector< double > temp_stats(params.size()); + std::vector< double > temp_stats; + temp_stats.reserve(params.size()); const std::vector< double > & stats = pset_stats[a]; int i_matches = -1; @@ -1317,7 +1319,7 @@ MODEL_TEMPLATE(Array_Type, sample)( // Filling out the parameters for (auto p = 0u; p < params.size(); ++p) - temp_stats[p] = stats[array * k + p]; + temp_stats.push_back(stats[array * k + p]); probs[array] = this->likelihood(params, temp_stats, i, false); cumprob += probs[array]; @@ -1364,9 +1366,10 @@ MODEL_TEMPLATE(double, conditional_prob)( A.insert_cell(i, j, A.default_val(), true, false); // Computing the change stats_target - std::vector< double > tmp_counts(counters->size()); - for (size_t ii = 0u; ii < tmp_counts.size(); ++ii) - tmp_counts[ii] = counters->operator[](ii).count(A, i, j); + std::vector< double > tmp_counts; + tmp_counts.reserve(counters->size()); + for (size_t ii = 0u; ii < counters->size(); ++ii) + tmp_counts.push_back(counters->operator[](ii).count(A, i, j)); // If there is a transformation function, it needs to be // applied before dealing with the likelihood. @@ -1490,7 +1493,7 @@ MODEL_TEMPLATE(void, set_transform_model)( // Applying it to the support for (auto s = 0u; s < pset_arrays.size(); ++s) { - std::vector< double > new_stats(0u); + std::vector< double > new_stats; for (auto a = 0u; a < pset_arrays[s].size(); ++a) { diff --git a/include/barry/models/defm/defm-meat.hpp b/include/barry/models/defm/defm-meat.hpp index 36ab4cfa6..926d53cc1 100644 --- a/include/barry/models/defm/defm-meat.hpp +++ b/include/barry/models/defm/defm-meat.hpp @@ -9,19 +9,20 @@ inline std::vector< double > keygen_defm( size_t nrow = Array_.nrow(); size_t ncol = Array_.ncol(); - std::vector< double > res( + std::vector< double > res; + res.reserve( 2u + // Rows + cols ncol * (nrow - 1u) // Markov cells ); - res[0u] = static_cast(nrow); - res[1u] = static_cast(ncol); + res.push_back(static_cast(nrow)); + res.push_back(static_cast(ncol)); - size_t iter = 2u; + // size_t iter = 2u; // Adding the cells for (size_t i = 0u; i < (nrow - 1); ++i) for (size_t j = 0u; j < ncol; ++j) - res[iter++] = Array_(i, j); + res.push_back(Array_(i, j)); return res; diff --git a/include/barry/models/geese/geese-meat-likelihood.hpp b/include/barry/models/geese/geese-meat-likelihood.hpp index 3d5637025..3e5edcbce 100644 --- a/include/barry/models/geese/geese-meat-likelihood.hpp +++ b/include/barry/models/geese/geese-meat-likelihood.hpp @@ -136,9 +136,10 @@ inline double Geese::likelihood( } // Multiplying by P(x|x_n), the transition probability - std::vector< double > temp_stats(par0.size(), 0.0); + std::vector< double > temp_stats; + temp_stats.reserve(par0.size()); for (auto p = 0u; p < par0.size(); ++p) - temp_stats[p] = psets_stats->operator[](par0.size() * nstate + p); + temp_stats.push_back(psets_stats->operator[](par0.size() * nstate + p)); nstate++; diff --git a/include/barry/models/geese/geese-meat-predict.hpp b/include/barry/models/geese/geese-meat-predict.hpp index f3bd16783..9d697b9d2 100644 --- a/include/barry/models/geese/geese-meat-predict.hpp +++ b/include/barry/models/geese/geese-meat-predict.hpp @@ -90,13 +90,17 @@ inline std::vector< std::vector > Geese::predict_backend( continue; // Creating space. - std::vector< std::vector< double > > everything_below(states.size()); - std::vector< std::vector< double > > everything_above(states.size()); + std::vector< std::vector< double > > everything_below; + std::vector< std::vector< double > > everything_above; + + everything_below.reserve(states.size()); + everything_above.reserve(states.size()); // All combinations of the the parent states // So psets[s] = combinations of offspring given state s. // psets[s][i] = The ith combination of offspring given state s. - std::vector< std::vector< PhyloArray > > psets(states.size()); + std::vector< std::vector< PhyloArray > > psets; + psets.reserve(states.size()); // Making space for the offspring for (auto & off : parent.offspring) @@ -108,6 +112,9 @@ inline std::vector< std::vector > Geese::predict_backend( // Iterating through the parent states for (size_t s = 0u; s < states.size(); ++s) { + std::vector< double > below; + std::vector< double > above; + std::vector< PhyloArray > pset; // Retrieving powerset of stats and arrays const auto & pset_arrays = model->get_pset(parent.narray[s]); @@ -175,15 +182,15 @@ inline std::vector< std::vector > Geese::predict_backend( if (!in_the_set) continue; - psets[s].push_back(array_p); // Generating a copy + pset.push_back(array_p); // Generating a copy // - With focal node, conditioning on it beening status s. // - But the offspring probabilities are the central ones here. // - So the saved values are for computing P(x_offspring | Data) - everything_below[s].push_back(everything_below_p); + below.push_back(everything_below_p); // The first run, we only need to grow the list - everything_above[s].push_back( + above.push_back( model->likelihood( par_terms, target_p, parent.narray[s], false ) * parent.probability[s] / parent.subtree_prob[s] @@ -191,6 +198,12 @@ inline std::vector< std::vector > Geese::predict_backend( } // end for psets + + // Storing the psets + psets.push_back(std::move(pset)); + everything_below.push_back(std::move(below)); + everything_above.push_back(std::move(above)); + } // end for states diff --git a/include/barry/rules-meat.hpp b/include/barry/rules-meat.hpp index 951a1813f..6413d5284 100644 --- a/include/barry/rules-meat.hpp +++ b/include/barry/rules-meat.hpp @@ -167,9 +167,10 @@ template inline std::vector Rules::get_names() const { - std::vector< std::string > out(this->size()); + std::vector< std::string > out; + out.reserve(this->size()); for (size_t i = 0u; i < out.size(); ++i) - out[i] = this->data.at(i).get_name(); + out.push_back(this->data.at(i).get_name()); return out; @@ -179,9 +180,10 @@ template inline std::vector Rules::get_descriptions() const { - std::vector< std::string > out(this->size()); + std::vector< std::string > out; + out.reserve(this->size()); for (size_t i = 0u; i < out.size(); ++i) - out[i] = data.at(i).get_description(); + out.push_back(data.at(i).get_description()); return out;