Skip to content

Commit

Permalink
improved cpp code
Browse files Browse the repository at this point in the history
  • Loading branch information
qddyy committed Dec 15, 2023
1 parent f845525 commit 11509bd
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 272 deletions.
36 changes: 13 additions & 23 deletions src/association_pmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

using namespace Rcpp;

inline void association_do(
unsigned& i,
inline bool association_update(
PermuBar& bar,
const NumericVector& x,
const NumericVector& y,
const Function& statistic_func,
NumericVector& statistic_permu,
ProgressBar& bar)
const Function& statistic_func)
{
statistic_permu[i] = as<double>(statistic_func(x, y));

bar.update(i);
i++;
return bar.update(as<double>(statistic_func(x, y)));
}

// [[Rcpp::export]]
Expand All @@ -23,26 +18,21 @@ NumericVector association_pmt(
const Function statistic_func,
const unsigned n_permu)
{
ProgressBar bar;
NumericVector statistic_permu;

unsigned i = 0;
if (n_permu == 0) {
std::tie(statistic_permu, bar) = statistic_permu_with_bar(n_permutation(y), true);
PermuBar bar(n_permutation(y), true);

do {
association_do(i, x, y, statistic_func, statistic_permu, bar);
association_update(bar, x, y, statistic_func);
} while (std::next_permutation(y.begin(), y.end()));

return bar.statistic_permu;
} else {
std::tie(statistic_permu, bar) = statistic_permu_with_bar(n_permu, false);
PermuBar bar(n_permu, false);

while (i < n_permu) {
do {
random_shuffle(y);
association_do(i, x, y, statistic_func, statistic_permu, bar);
}
}

bar.done();
} while (association_update(bar, x, y, statistic_func));

return statistic_permu;
return bar.statistic_permu;
}
}
35 changes: 13 additions & 22 deletions src/ksample_pmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

using namespace Rcpp;

inline void ksample_do(
unsigned& i,
inline bool ksample_update(
PermuBar& bar,
const NumericVector& data,
const IntegerVector& group,
const Function& statistic_func,
NumericVector& statistic_permu,
ProgressBar& bar)
const Function& statistic_func)
{
statistic_permu[i] = as<double>(statistic_func(data, group));

bar.update(i);
i++;
return bar.update(as<double>(statistic_func(data, group)));
}

// [[Rcpp::export]]
Expand All @@ -23,26 +18,22 @@ NumericVector ksample_pmt(
const Function statistic_func,
const unsigned n_permu)
{
ProgressBar bar;
NumericVector statistic_permu;

unsigned i = 0;
if (n_permu == 0) {
std::tie(statistic_permu, bar) = statistic_permu_with_bar(n_permutation(group), true);
PermuBar bar(n_permutation(group), true);

do {
ksample_do(i, data, group, statistic_func, statistic_permu, bar);
ksample_update(bar, data, group, statistic_func);
} while (std::next_permutation(group.begin(), group.end()));

return bar.statistic_permu;
} else {
std::tie(statistic_permu, bar) = statistic_permu_with_bar(n_permu, false);
PermuBar bar(n_permu, false);

while (i < n_permu) {
do {
random_shuffle(group);
ksample_do(i, data, group, statistic_func, statistic_permu, bar);
}
}

bar.done();
} while (ksample_update(bar, data, group, statistic_func));

return statistic_permu;
return bar.statistic_permu;
}
}
49 changes: 22 additions & 27 deletions src/multicomp_pmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@

using namespace Rcpp;

inline void multicomp_do(
unsigned& i,
const unsigned& n,
inline bool multicomp_update(
PermuBar& bar,
const unsigned& n_pair,
const unsigned& n_group,
const IntegerVector& group_i,
const IntegerVector& group_j,
const NumericVector& data,
const IntegerVector& group,
const Function& statistic_func,
NumericVector& statistic_permu,
ProgressBar& bar, List& split)
List& split)
{
for (unsigned j = 1; j <= n; j++) {
split[j - 1] = data[group == j];
for (unsigned i = 1; i <= n_group; i++) {
split[i - 1] = data[group == i];
}

for (unsigned k = 0; k < n_pair; k++) {
statistic_permu(k, i) = as<double>(statistic_func(split[group_i[k]], split[group_j[k]], data, group));
unsigned j;
for (j = 0; j < n_pair - 1; j++) {
bar.update(as<double>(statistic_func(split[group_i[j]], split[group_j[j]], data, group)));
}

bar.update(i);
i++;
return bar.update(as<double>(statistic_func(split[group_i[j]], split[group_j[j]], data, group)));
}

// [[Rcpp::export]]
Expand All @@ -35,30 +34,26 @@ NumericVector multicomp_pmt(
const Function statistic_func,
const unsigned n_permu)
{
ProgressBar bar;
NumericVector statistic_permu;
unsigned n_group = group[group.size() - 1];
unsigned n_pair = n_group * (n_group - 1) / 2;

unsigned n_pair = group_i.size();
unsigned n = group[group.size() - 1];
List split(n);
List split(n_group);

unsigned i = 0;
if (n_permu == 0) {
std::tie(statistic_permu, bar) = statistic_permu_with_bar(n_permutation(group), true, n_pair);
PermuBar bar(n_permutation(group), true, n_pair);

do {
multicomp_do(i, n, n_pair, group_i, group_j, data, group, statistic_func, statistic_permu, bar, split);
multicomp_update(bar, n_pair, n_group, group_i, group_j, data, group, statistic_func, split);
} while (std::next_permutation(group.begin(), group.end()));

return bar.statistic_permu;
} else {
std::tie(statistic_permu, bar) = statistic_permu_with_bar(n_permu, false, n_pair);
PermuBar bar(n_permu, false, n_pair);

while (i < n_permu) {
do {
random_shuffle(group);
multicomp_do(i, n, n_pair, group_i, group_j, data, group, statistic_func, statistic_permu, bar, split);
}
}
} while (multicomp_update(bar, n_pair, n_group, group_i, group_j, data, group, statistic_func, split));

bar.done();

return statistic_permu;
return bar.statistic_permu;
}
}
43 changes: 19 additions & 24 deletions src/paired_pmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@

using namespace Rcpp;

inline void paired_do(
unsigned& i,
const unsigned& k,
inline bool paired_update(
PermuBar& bar,
const unsigned& i,
const unsigned& n,
const Function& statistic_func,
NumericVector& statistic_permu,
ProgressBar& bar, LogicalVector& swapped)
LogicalVector& swapped)
{
for (unsigned j = 0; j < n; j++) {
swapped[j] = ((i & (1 << j)) != 0);
}

statistic_permu[i] = as<double>(statistic_func(swapped));

bar.update(i);
i++;
return bar.update(as<double>(statistic_func(swapped)));
}

// [[Rcpp::export]]
Expand All @@ -26,28 +22,27 @@ NumericVector paired_pmt(
const Function statistic_func,
const unsigned n_permu)
{
ProgressBar bar;
NumericVector statistic_permu;
unsigned total = (1 << n);

LogicalVector swapped(n);
unsigned total = (1 << n);

unsigned i = 0;
if (n_permu == 0) {
std::tie(statistic_permu, bar) = statistic_permu_with_bar(total, true);
PermuBar bar(total, true);

while (i < total) {
paired_do(i, i, n, statistic_func, statistic_permu, bar, swapped);
}
} else {
std::tie(statistic_permu, bar) = statistic_permu_with_bar(n_permu, false);
do {
paired_update(bar, i, n, statistic_func, swapped);
i++;
} while (i < total);

while (i < n_permu) {
paired_do(i, rand_int(total), n, statistic_func, statistic_permu, bar, swapped);
}
}
return bar.statistic_permu;
} else {
PermuBar bar(n_permu, false);

bar.done();
do {
i = rand_int(total);
} while (paired_update(bar, i, n, statistic_func, swapped));

return statistic_permu;
return bar.statistic_permu;
}
}
56 changes: 24 additions & 32 deletions src/rcbd_pmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

using namespace Rcpp;

inline void rcbd_do(
unsigned& i,
inline bool rcbd_update(
PermuBar& bar,
const NumericMatrix& data,
const Function& statistic_func,
NumericVector& statistic_permu,
ProgressBar& bar)
const Function& statistic_func
)
{
statistic_permu[i] = as<double>(statistic_func(data));

bar.update(i);
i++;
return bar.update(as<double>(statistic_func(data)));
}

// [[Rcpp::export]]
Expand All @@ -21,43 +17,39 @@ NumericVector rcbd_pmt(
const Function statistic_func,
const unsigned n_permu)
{
ProgressBar bar;
NumericVector statistic_permu;

unsigned n_col = data.ncol();

unsigned i = 0;
unsigned j = 0;
if (n_permu == 0) {
unsigned total = 1;
for (unsigned k = 0; k < n_col; k++) {
total *= n_permutation(data.column(k));
for (unsigned j = 0; j < n_col; j++) {
total *= n_permutation(data.column(j));
}
std::tie(statistic_permu, bar) = statistic_permu_with_bar(total, true);

while (j < n_col) {
if (j == 0) {
rcbd_do(i, data, statistic_func, statistic_permu, bar);
PermuBar bar(total, true);

while (i < n_col) {
if (i == 0) {
rcbd_update(bar, data, statistic_func);
}

if (std::next_permutation(data.column(j).begin(), data.column(j).end())) {
j = 0;
if (std::next_permutation(data.column(i).begin(), data.column(i).end())) {
i = 0;
} else {
j++;
i++;
}
}

return bar.statistic_permu;
} else {
std::tie(statistic_permu, bar) = statistic_permu_with_bar(n_permu, false);
PermuBar bar(n_permu, false);

while (i < n_permu) {
for (j = 0; j < n_col; j++) {
random_shuffle(data.column(j));
do {
for (i = 0; i < n_col; i++) {
random_shuffle(data.column(i));
}
rcbd_do(i, data, statistic_func, statistic_permu, bar);
}
}
} while (rcbd_update(bar, data, statistic_func));

bar.done();

return statistic_permu;
return bar.statistic_permu;
}
}
Loading

0 comments on commit 11509bd

Please sign in to comment.