Skip to content

Commit

Permalink
Using reserve whenever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Sep 26, 2023
1 parent 66d19b5 commit f3aa5ce
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 54 deletions.
43 changes: 25 additions & 18 deletions barry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5394,9 +5394,10 @@ COUNTERS_TEMPLATE(void, add_counter)(
COUNTERS_TEMPLATE(std::vector<std::string>, 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;

Expand All @@ -5405,9 +5406,10 @@ COUNTERS_TEMPLATE(std::vector<std::string>, get_names)() const
COUNTERS_TEMPLATE(std::vector<std::string>, 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;

Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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;
Expand All @@ -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];
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -9335,9 +9340,10 @@ template<typename Array_Type, typename Data_Type>
inline std::vector<std::string> Rules<Array_Type, Data_Type>::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;

Expand All @@ -9347,9 +9353,10 @@ template<typename Array_Type, typename Data_Type>
inline std::vector<std::string> Rules<Array_Type, Data_Type>::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;

Expand Down
11 changes: 6 additions & 5 deletions defm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(nrow);
res[1u] = static_cast<double>(ncol);
res.push_back(static_cast<double>(nrow));
res.push_back(static_cast<double>(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;

Expand Down
14 changes: 8 additions & 6 deletions include/barry/counters-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,10 @@ COUNTERS_TEMPLATE(void, add_counter)(
COUNTERS_TEMPLATE(std::vector<std::string>, 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;

Expand All @@ -212,9 +213,10 @@ COUNTERS_TEMPLATE(std::vector<std::string>, get_names)() const
COUNTERS_TEMPLATE(std::vector<std::string>, 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;

Expand Down
19 changes: 11 additions & 8 deletions include/barry/model-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -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;
Expand All @@ -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];
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
{
Expand Down
11 changes: 6 additions & 5 deletions include/barry/models/defm/defm-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(nrow);
res[1u] = static_cast<double>(ncol);
res.push_back(static_cast<double>(nrow));
res.push_back(static_cast<double>(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;

Expand Down
5 changes: 3 additions & 2 deletions include/barry/models/geese/geese-meat-likelihood.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;

Expand Down
25 changes: 19 additions & 6 deletions include/barry/models/geese/geese-meat-predict.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,17 @@ inline std::vector< std::vector<double> > 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)
Expand All @@ -108,6 +112,9 @@ inline std::vector< std::vector<double> > 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]);
Expand Down Expand Up @@ -175,22 +182,28 @@ inline std::vector< std::vector<double> > 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]
);


} // 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

Expand Down
10 changes: 6 additions & 4 deletions include/barry/rules-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ template<typename Array_Type, typename Data_Type>
inline std::vector<std::string> Rules<Array_Type, Data_Type>::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;

Expand All @@ -179,9 +180,10 @@ template<typename Array_Type, typename Data_Type>
inline std::vector<std::string> Rules<Array_Type, Data_Type>::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;

Expand Down

0 comments on commit f3aa5ce

Please sign in to comment.