Skip to content

Commit

Permalink
improve cpp code
Browse files Browse the repository at this point in the history
  • Loading branch information
qddyy committed Jan 3, 2025
1 parent b08c987 commit 0d4f196
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 33 deletions.
3 changes: 1 addition & 2 deletions inst/include/pmt/impl_association_pmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ RObject impl_association_pmt(
{
Stat<progress> statistic_container;

auto statistic_closure = statistic_func(x, y);
auto association_update = [x, y, &statistic_closure, &statistic_container]() {
auto association_update = [&statistic_container, statistic_closure = statistic_func(x, y), x, y]() {
return statistic_container << statistic_closure(x, y);
};

Expand Down
3 changes: 1 addition & 2 deletions inst/include/pmt/impl_ksample_pmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ RObject impl_ksample_pmt(
{
Stat<progress> statistic_container;

auto statistic_closure = statistic_func(data, group);
auto ksample_update = [data, group, &statistic_closure, &statistic_container]() {
auto ksample_update = [&statistic_container, statistic_closure = statistic_func(data, group), data, group]() {
return statistic_container << statistic_closure(data, group);
};

Expand Down
9 changes: 4 additions & 5 deletions inst/include/pmt/impl_multcomp_pmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ RObject impl_multcomp_pmt(
const T& statistic_func,
const double n_permu)
{
R_xlen_t n_group = group[group.size() - 1];
R_xlen_t n_pair = n_group * (n_group - 1) / 2;
R_xlen_t K = group_i.size();

Stat<progress> statistic_container(n_pair);
Stat<progress> statistic_container(K);

auto multcomp_update = [group_i, group_j, data, group, n_pair, &statistic_func, &statistic_container]() {
auto multcomp_update = [&statistic_container, &statistic_func, group_i, group_j, data, group, K]() {
auto statistic_closure = statistic_func(data, group);

bool flag = false;
for (R_xlen_t k = 0; k < n_pair; k++) {
for (R_xlen_t k = 0; k < K; k++) {
flag = statistic_container << statistic_closure(group_i[k], group_j[k]);
};

Expand Down
3 changes: 1 addition & 2 deletions inst/include/pmt/impl_paired_pmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ RObject impl_paired_pmt(
{
Stat<progress> statistic_container;

auto statistic_closure = statistic_func(x, y);
auto paired_update = [x, y, &statistic_closure, &statistic_container]() {
auto paired_update = [&statistic_container, statistic_closure = statistic_func(x, y), x, y]() {
return statistic_container << statistic_closure(x, y);
};

Expand Down
3 changes: 1 addition & 2 deletions inst/include/pmt/impl_rcbd_pmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ RObject impl_rcbd_pmt(
{
Stat<progress> statistic_container;

auto statistic_closure = statistic_func(data);
auto rcbd_update = [data, &statistic_closure, &statistic_container]() {
auto rcbd_update = [&statistic_container, statistic_closure = statistic_func(data), data]() {
return statistic_container << statistic_closure(data);
};

Expand Down
17 changes: 6 additions & 11 deletions inst/include/pmt/impl_table_pmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@ RObject impl_table_pmt(
{
Stat<progress> statistic_container;

R_xlen_t n = row.size();

IntegerMatrix data(no_init(row[n - 1] + 1, col[n - 1] + 1));

auto data_filled = [data, row, col, n]() mutable {
data.fill(0);
auto data = [_data = IntegerMatrix(no_init(*(row.end() - 1) + 1, *(col.end() - 1) + 1)), row, col, n = row.size()]() mutable {
_data.fill(0);
for (R_xlen_t i = 0; i < n; i++) {
data(row[i], col[i])++;
_data(row[i], col[i])++;
}
return data;
return _data;
};

auto statistic_closure = statistic_func(data_filled());
auto table_update = [&data_filled, &statistic_closure, &statistic_container]() {
return statistic_container << statistic_closure(data_filled());
auto table_update = [&statistic_container, statistic_closure = statistic_func(data()), &data]() {
return statistic_container << statistic_closure(data());
};

statistic_container.init_statistic(table_update);
Expand Down
3 changes: 1 addition & 2 deletions inst/include/pmt/impl_twosample_pmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ RObject impl_twosample_pmt(
{
Stat<progress> statistic_container;

auto statistic_closure = statistic_func(x, y);
auto twosample_update = [x, y, &statistic_closure, &statistic_container]() {
auto twosample_update = [&statistic_container, statistic_closure = statistic_func(x, y), x, y]() {
return statistic_container << statistic_closure(x, y);
};

Expand Down
15 changes: 8 additions & 7 deletions src/pmt_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

using namespace Rcpp;

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

#include <type_traits>
template <bool>
struct Tag { };

template <unsigned n>
constexpr auto Rf_lang = nullptr;
Expand All @@ -24,27 +22,30 @@ class StatFunc : public Function {
template <typename... Args>
auto operator()(Args&&... args) const
{
return _invoke(std::integral_constant<bool, sharing_args>(), std::forward<Args>(args)...);
return _invoke(Tag<sharing_args>(), std::forward<Args>(args)...);
}

private:
template <typename... Args>
auto _invoke(std::false_type, Args&&... args) const
auto _invoke(Tag<false>, Args&&... args) const
{
return [r_closure = Function(Function::operator()(std::forward<Args>(args)...))](auto&&... args) {
return as<double>(r_closure(std::forward<decltype(args)>(args)...));
};
}

template <typename... Args>
auto _invoke(std::true_type, Args&&... args) const
auto _invoke(Tag<true>, Args&&... args) const
{
return [r_call = Shield<SEXP>(Rf_lang<sizeof...(args) + 1>(Function::operator()(std::forward<Args>(args)...), std::forward<Args>(args)...))](auto&&...) {
return as<double>(Rcpp_fast_eval(r_call, R_GlobalEnv));
};
}
};

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

#include "pmt/impl_twosample_pmt.hpp"

// [[Rcpp::export]]
Expand Down

0 comments on commit 0d4f196

Please sign in to comment.