From f0efb222e6f988e34056f4e0b7afe023fa0bbb86 Mon Sep 17 00:00:00 2001 From: "George G. Vega Yon" Date: Tue, 26 Sep 2023 23:04:51 -0600 Subject: [PATCH] Code efficiency --- include/barry/barraydense-bones.hpp | 4 +- include/barry/barraydense-meat.hpp | 56 ++++++++++--------- include/barry/models/geese/geese-bones.hpp | 15 +++-- .../barry/models/geese/geese-meat-predict.hpp | 2 +- 4 files changed, 40 insertions(+), 37 deletions(-) diff --git a/include/barry/barraydense-bones.hpp b/include/barry/barraydense-bones.hpp index 8d4a53e76..3da38d471 100644 --- a/include/barry/barraydense-bones.hpp +++ b/include/barry/barraydense-bones.hpp @@ -194,9 +194,9 @@ class BArrayDense { void rm_cell(size_t i, size_t j, bool check_bounds = true, bool check_exists = true); - void insert_cell(size_t i, size_t j, const Cell< Cell_Type > & v, bool check_bounds, bool check_exists); + void insert_cell(size_t i, size_t j, const Cell< Cell_Type > & v, bool check_bounds, bool); // void insert_cell(size_t i, size_t j, Cell< Cell_Type > && v, bool check_bounds, bool check_exists); - void insert_cell(size_t i, size_t j, Cell_Type v, bool check_bounds, bool check_exists); + void insert_cell(size_t i, size_t j, Cell_Type v, bool check_bounds, bool); void swap_cells( size_t i0, size_t j0, size_t i1, size_t j1, bool check_bounds = true, diff --git a/include/barry/barraydense-meat.hpp b/include/barry/barraydense-meat.hpp index aedd5bc6b..57c2bc62a 100644 --- a/include/barry/barraydense-meat.hpp +++ b/include/barry/barraydense-meat.hpp @@ -153,13 +153,16 @@ inline BArrayDense:: BArrayDense( ) : N(Array_.N), M(Array_.M){ // Dimensions - el.resize(0u); - el_rowsums.resize(0u); - el_colsums.resize(0u); + el = Array_.el; + el_rowsums = Array_.el_rowsums; + el_colsums = Array_.el_colsums; + // el.resize(0u); + // el_rowsums.resize(0u); + // el_colsums.resize(0u); - std::copy(Array_.el.begin(), Array_.el.end(), std::back_inserter(el)); - std::copy(Array_.el_rowsums.begin(), Array_.el_rowsums.end(), std::back_inserter(el_rowsums)); - std::copy(Array_.el_colsums.begin(), Array_.el_colsums.end(), std::back_inserter(el_colsums)); + // std::copy(Array_.el.begin(), Array_.el.end(), std::back_inserter(el)); + // std::copy(Array_.el_rowsums.begin(), Array_.el_rowsums.end(), std::back_inserter(el_rowsums)); + // std::copy(Array_.el_colsums.begin(), Array_.el_colsums.end(), std::back_inserter(el_colsums)); // this->NCells = Array_.NCells; this->visited = Array_.visited; @@ -196,14 +199,17 @@ inline BArrayDense & BArrayDense::ope if (this != &Array_) { - el.resize(0u); - el_rowsums.resize(0u); - el_colsums.resize(0u); + el = Array_.el; + el_rowsums = Array_.el_rowsums; + el_colsums = Array_.el_colsums; + // el.resize(0u); + // el_rowsums.resize(0u); + // el_colsums.resize(0u); - // Entries - std::copy(Array_.el.begin(), Array_.el.end(), std::back_inserter(el)); - std::copy(Array_.el_rowsums.begin(), Array_.el_rowsums.end(), std::back_inserter(el_rowsums)); - std::copy(Array_.el_colsums.begin(), Array_.el_colsums.end(), std::back_inserter(el_colsums)); + // // Entries + // std::copy(Array_.el.begin(), Array_.el.end(), std::back_inserter(el)); + // std::copy(Array_.el_rowsums.begin(), Array_.el_rowsums.end(), std::back_inserter(el_rowsums)); + // std::copy(Array_.el_colsums.begin(), Array_.el_colsums.end(), std::back_inserter(el_colsums)); // this->NCells = Array_.NCells; @@ -406,9 +412,10 @@ inline std::vector< Cell_Type > BArrayDense::get_row_vec ( if (check_bounds) out_of_range(i, 0u); - std::vector< Cell_Type > ans(ncol(), static_cast< Cell_Type >(false)); + std::vector< Cell_Type > ans; + ans.reserve(ncol()); for (size_t j = 0u; j < M; ++j) - ans[j] = el[POS(i, j)]; + ans.push_back(el[POS(i, j)]); return ans; @@ -425,7 +432,7 @@ template inline void BArrayDenseat(j) = el[POS(i, j)]; + x->operator[](j) = el[POS(i, j)]; } @@ -438,9 +445,10 @@ template inline std::vector< Cell_Type > if (check_bounds) out_of_range(0u, i); - std::vector< Cell_Type > ans(nrow(), static_cast< Cell_Type >(false)); + std::vector< Cell_Type > ans; + ans.reserve(nrow()); for (size_t j = 0u; j < N; ++j) - ans[j] = el[POS(j, i)]; + ans.push_back(el[POS(j, i)]); return ans; @@ -457,7 +465,7 @@ template inline void BArrayDenseat(j) = el[POS(j, i)];//this->get_cell(iter->first, i, false); + x->operator[](j) = el[POS(j, i)];//this->get_cell(iter->first, i, false); } template @@ -655,7 +663,7 @@ inline void BArrayDense::rm_cell ( if (check_bounds) out_of_range(i,j); - BARRY_UNUSED(check_exists) + // BARRY_UNUSED(check_exists) // Remove the pointer first (so it wont point to empty) el_rowsums[i] -= el[POS(i, j)]; @@ -672,13 +680,11 @@ inline void BArrayDense::insert_cell ( size_t j, const Cell< Cell_Type> & v, bool check_bounds, - bool check_exists + bool ) { if (check_bounds) out_of_range(i,j); - - BARRY_UNUSED(check_exists) if (el[POS(i,j)] == BARRY_ZERO_DENSE) { @@ -708,13 +714,11 @@ template inline void BArrayDense keygen_full( // Baseline data: nrows and columns std::vector< double > dat = { static_cast(array.nrow()) * 100000 + - static_cast(array.ncol()) + static_cast(array.ncol()), + 1000000.0, // state of the parent + array.D_ptr()->duplication ? 1.0 : 0.0 // type of the parent }; // State of the parent - dat.push_back(1000000.0); - size_t count = 0u; + double pow10 = 1.0; for (bool i : array.D_ptr()->states) { - dat[dat.size() - 1u] += (i ? 1.0 : 0.0) * pow(10, static_cast(count)); - count++; + dat[1u] += (i ? 1.0 : 0.0) * pow10; + pow10 *= 10.0; } - // Type of the parent - dat.push_back(array.D_ptr()->duplication ? 1.0 : 0.0); - return dat; + } inline bool vec_diff( diff --git a/include/barry/models/geese/geese-meat-predict.hpp b/include/barry/models/geese/geese-meat-predict.hpp index 9d697b9d2..5c5cff186 100644 --- a/include/barry/models/geese/geese-meat-predict.hpp +++ b/include/barry/models/geese/geese-meat-predict.hpp @@ -170,7 +170,7 @@ inline std::vector< std::vector > Geese::predict_backend( // Getting the offspring state, and how it maps, only // if it is not an offspring const auto & off_state = array_p.get_col_vec(off); - size_t loc = this->map_to_state_id[off_state]; + size_t loc = this->map_to_state_id.find(off_state)->second; everything_below_p *= parent.offspring[off]->subtree_prob[loc];