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 4, 2024
1 parent d2ac38a commit 191dbfb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
21 changes: 9 additions & 12 deletions inst/include/pmt/impl_rcbd_pmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,31 @@ NumericVector impl_rcbd_pmt(
bar.init_statistic(rcbd_update);

if (!std::isnan(n_permu)) {
R_len_t i = 0;
R_len_t n_block = data.ncol();
R_len_t i;
R_len_t b = data.ncol();

if (n_permu == 0) {
double total = 1;
for (R_len_t j = 0; j < n_block; j++) {
std::sort(data.column(j).begin(), data.column(j).end());
total *= n_permutation(data.column(j));
for (i = 0; i < b; i++) {
std::sort(data.column(i).begin(), data.column(i).end());
total *= n_permutation(data.column(i));
}

bar.init_statistic_permu(total);

while (i < n_block) {
i = 0;
while (i < b) {
if (i == 0) {
rcbd_update();
}

if (next_permutation(data.column(i))) {
i = 0;
} else {
i++;
}
i = next_permutation(data.column(i)) ? 0 : i + 1;
}
} else {
bar.init_statistic_permu(n_permu);

do {
for (i = 0; i < n_block; i++) {
for (i = 0; i < b; i++) {
random_shuffle(data.column(i));
}
} while (rcbd_update());
Expand Down
6 changes: 3 additions & 3 deletions inst/include/pmt/reorder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ T rand_int(T n)
}

template <typename T>
void random_shuffle(T v)
void random_shuffle(T&& v)
{
R_len_t n = v.size();

Expand All @@ -21,13 +21,13 @@ void random_shuffle(T v)
}

template <typename T>
bool next_permutation(T v)
bool next_permutation(T&& v)
{
return std::next_permutation(v.begin(), v.end());
}

template <typename T>
double n_permutation(T v)
double n_permutation(const T& v)
{
double A = 1;

Expand Down
38 changes: 19 additions & 19 deletions src/pmt_interface.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include <Rcpp.h>
#include <Rcpp/Lightest>

using namespace Rcpp;

#include "pmt/progress.hpp"
#include "pmt/reorder.hpp"

class ClosFunc : public Function {
class StatFunc : public Function {
public:
using Function::Function;

template <typename... Args>
auto operator()(Args&&... args) const
{
return [closure = Function(Function::operator()(std::forward<Args>(args)...))](auto&&... args) {
return as<double>(closure(std::forward<decltype(args)>(args)...));
return [r_closure = Function(Function::operator()(std::forward<Args>(args)...))](auto&&... args) {
return as<double>(r_closure(std::forward<decltype(args)>(args)...));
};
}
};
Expand All @@ -29,8 +29,8 @@ SEXP twosample_pmt(
const bool progress)
{
return progress ?
impl_twosample_pmt<PermuBarShow, ClosFunc>(clone(x), clone(y), statistic_func, n_permu) :
impl_twosample_pmt<PermuBarHide, ClosFunc>(clone(x), clone(y), statistic_func, n_permu);
impl_twosample_pmt<PermuBarShow, StatFunc>(clone(x), clone(y), statistic_func, n_permu) :
impl_twosample_pmt<PermuBarHide, StatFunc>(clone(x), clone(y), statistic_func, n_permu);
}

#include "pmt/impl_ksample_pmt.hpp"
Expand All @@ -44,8 +44,8 @@ SEXP ksample_pmt(
const bool progress)
{
return progress ?
impl_ksample_pmt<PermuBarShow, ClosFunc>(data, clone(group), statistic_func, n_permu) :
impl_ksample_pmt<PermuBarHide, ClosFunc>(data, clone(group), statistic_func, n_permu);
impl_ksample_pmt<PermuBarShow, StatFunc>(data, clone(group), statistic_func, n_permu) :
impl_ksample_pmt<PermuBarHide, StatFunc>(data, clone(group), statistic_func, n_permu);
}

#include "pmt/impl_multcomp_pmt.hpp"
Expand All @@ -61,8 +61,8 @@ SEXP multcomp_pmt(
const bool progress)
{
return progress ?
impl_multcomp_pmt<PermuBarShow, ClosFunc>(group_i, group_j, data, clone(group), statistic_func, n_permu) :
impl_multcomp_pmt<PermuBarHide, ClosFunc>(group_i, group_j, data, clone(group), statistic_func, n_permu);
impl_multcomp_pmt<PermuBarShow, StatFunc>(group_i, group_j, data, clone(group), statistic_func, n_permu) :
impl_multcomp_pmt<PermuBarHide, StatFunc>(group_i, group_j, data, clone(group), statistic_func, n_permu);
}

#include "pmt/impl_paired_pmt.hpp"
Expand All @@ -76,8 +76,8 @@ SEXP paired_pmt(
const bool progress)
{
return progress ?
impl_paired_pmt<PermuBarShow, ClosFunc>(clone(x), clone(y), statistic_func, n_permu) :
impl_paired_pmt<PermuBarHide, ClosFunc>(clone(x), clone(y), statistic_func, n_permu);
impl_paired_pmt<PermuBarShow, StatFunc>(clone(x), clone(y), statistic_func, n_permu) :
impl_paired_pmt<PermuBarHide, StatFunc>(clone(x), clone(y), statistic_func, n_permu);
}

#include "pmt/impl_rcbd_pmt.hpp"
Expand All @@ -90,8 +90,8 @@ SEXP rcbd_pmt(
const bool progress)
{
return progress ?
impl_rcbd_pmt<PermuBarShow, ClosFunc>(clone(data), statistic_func, n_permu) :
impl_rcbd_pmt<PermuBarHide, ClosFunc>(clone(data), statistic_func, n_permu);
impl_rcbd_pmt<PermuBarShow, StatFunc>(clone(data), statistic_func, n_permu) :
impl_rcbd_pmt<PermuBarHide, StatFunc>(clone(data), statistic_func, n_permu);
}

#include "pmt/impl_association_pmt.hpp"
Expand All @@ -105,8 +105,8 @@ SEXP association_pmt(
const bool progress)
{
return progress ?
impl_association_pmt<PermuBarShow, ClosFunc>(x, clone(y), statistic_func, n_permu) :
impl_association_pmt<PermuBarHide, ClosFunc>(x, clone(y), statistic_func, n_permu);
impl_association_pmt<PermuBarShow, StatFunc>(x, clone(y), statistic_func, n_permu) :
impl_association_pmt<PermuBarHide, StatFunc>(x, clone(y), statistic_func, n_permu);
}

#include "pmt/impl_table_pmt.hpp"
Expand All @@ -120,6 +120,6 @@ SEXP table_pmt(
const bool progress)
{
return progress ?
impl_table_pmt<PermuBarShow, ClosFunc>(row, clone(col), statistic_func, n_permu) :
impl_table_pmt<PermuBarHide, ClosFunc>(row, clone(col), statistic_func, n_permu);
}
impl_table_pmt<PermuBarShow, StatFunc>(row, clone(col), statistic_func, n_permu) :
impl_table_pmt<PermuBarHide, StatFunc>(row, clone(col), statistic_func, n_permu);
}

0 comments on commit 191dbfb

Please sign in to comment.