Skip to content

Commit

Permalink
Code efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Sep 27, 2023
1 parent f3aa5ce commit f0efb22
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
4 changes: 2 additions & 2 deletions include/barry/barraydense-bones.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
56 changes: 30 additions & 26 deletions include/barry/barraydense-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,16 @@ inline BArrayDense<Cell_Type, Data_Type>:: 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;
Expand Down Expand Up @@ -196,14 +199,17 @@ inline BArrayDense<Cell_Type,Data_Type> & BArrayDense<Cell_Type, Data_Type>::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;
Expand Down Expand Up @@ -406,9 +412,10 @@ inline std::vector< Cell_Type > BArrayDense<Cell_Type, Data_Type>::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;

Expand All @@ -425,7 +432,7 @@ template<typename Cell_Type, typename Data_Type> inline void BArrayDense<Cell_Ty
out_of_range(i, 0u);

for (size_t j = 0u; j < M; ++j)
x->at(j) = el[POS(i, j)];
x->operator[](j) = el[POS(i, j)];

}

Expand All @@ -438,9 +445,10 @@ template<typename Cell_Type, typename Data_Type> 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;

Expand All @@ -457,7 +465,7 @@ template<typename Cell_Type, typename Data_Type> inline void BArrayDense<Cell_Ty
out_of_range(0u, i);

for (size_t j = 0u; j < N; ++j)
x->at(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<typename Cell_Type, typename Data_Type>
Expand Down Expand Up @@ -655,7 +663,7 @@ inline void BArrayDense<Cell_Type, Data_Type>::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)];
Expand All @@ -672,13 +680,11 @@ inline void BArrayDense<Cell_Type, Data_Type>::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)
{
Expand Down Expand Up @@ -708,13 +714,11 @@ template<typename Cell_Type, typename Data_Type> inline void BArrayDense<Cell_Ty
size_t j,
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)
{
Expand Down
15 changes: 7 additions & 8 deletions include/barry/models/geese/geese-bones.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,20 @@ inline std::vector< double > keygen_full(
// Baseline data: nrows and columns
std::vector< double > dat = {
static_cast<double>(array.nrow()) * 100000 +
static_cast<double>(array.ncol())
static_cast<double>(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<double>(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(
Expand Down
2 changes: 1 addition & 1 deletion include/barry/models/geese/geese-meat-predict.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ inline std::vector< std::vector<double> > 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];

Expand Down

0 comments on commit f0efb22

Please sign in to comment.