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 7, 2024
1 parent d2ac38a commit 2a6b855
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 51 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
13 changes: 6 additions & 7 deletions inst/include/pmt/impl_twosample_pmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ NumericVector impl_twosample_pmt(

bar.init_statistic(twosample_update);

// permuting based on the shorter
NumericVector x_ = x.size() < y.size() ? x : y;
NumericVector y_ = x.size() < y.size() ? y : x;
if (!std::isnan(n_permu)) {
// permuting based on the shorter
NumericVector x_ = x.size() < y.size() ? x : y;
NumericVector y_ = x.size() < y.size() ? y : x;

R_len_t m = x_.size();
R_len_t n = x_.size() + y_.size();
R_len_t m = x_.size();
R_len_t n = x_.size() + y_.size();

if (!std::isnan(n_permu)) {
R_len_t i, j;

if (n_permu == 0) {
IntegerVector p(n, 0);

Expand Down
20 changes: 10 additions & 10 deletions inst/include/pmt/progress.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@

constexpr unsigned bar_width = 50;

using ProgressBar = std::array<char, bar_width + 19>;
using progress_bar = std::array<char, bar_width + 19>;

constexpr std::array<char, 10> num_char_map = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

template <unsigned n, unsigned... Is>
constexpr auto generate_bar(std::integer_sequence<unsigned, Is...>)
template <unsigned n, unsigned... seq>
constexpr auto generate_bar(std::integer_sequence<unsigned, seq...>)
{
unsigned fill = n * bar_width / 100;
return ProgressBar {
constexpr unsigned fill = n * bar_width / 100;
return progress_bar {
'\015',
'\033', '[', '3', '1', 'm',
(n < 10) ? ' ' : num_char_map[n / 10],
(n < 10) ? num_char_map[n] : num_char_map[n % 10], '%',
'\033', '[', '3', '6', 'm',
' ', '|', (Is + 1 < fill ? '-' : (Is + 1 == fill ? '>' : ' '))..., '|', ' ',
' ', '|', (seq + 1 < fill ? '-' : (seq + 1 == fill ? '>' : ' '))..., '|', ' ',
'\0'
};
}

template <unsigned... Is>
constexpr auto generate_bars(std::integer_sequence<unsigned, Is...>)
template <unsigned... seq>
constexpr auto generate_bars(std::integer_sequence<unsigned, seq...>)
{
return std::array<ProgressBar, sizeof...(Is)> {
generate_bar<Is>(std::make_integer_sequence<unsigned, bar_width>())...
return std::array<progress_bar, sizeof...(seq)> {
generate_bar<seq>(std::make_integer_sequence<unsigned, bar_width>())...
};
}

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 2a6b855

Please sign in to comment.