From e9ade5341c48ae1f71989dd40935835b1cae6387 Mon Sep 17 00:00:00 2001 From: qddyy Date: Thu, 19 Dec 2024 15:47:42 +0800 Subject: [PATCH] improve `StatFunc` --- src/pmt_interface.cpp | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/pmt_interface.cpp b/src/pmt_interface.cpp index 2097644..a131f0a 100644 --- a/src/pmt_interface.cpp +++ b/src/pmt_interface.cpp @@ -7,9 +7,6 @@ using namespace Rcpp; #include -template -using tag_t = std::integral_constant; - template constexpr auto Rf_lang = nullptr; @@ -19,7 +16,7 @@ constexpr auto Rf_lang<2> = Rf_lang2; template <> constexpr auto Rf_lang<3> = Rf_lang3; -template +template class StatFunc : public Function { public: using Function::Function; @@ -27,22 +24,22 @@ class StatFunc : public Function { template auto operator()(Args&&... args) const { - return _invoke(tag_t(), std::forward(args)...); + return _invoke(std::integral_constant(), std::forward(args)...); } private: template - auto _invoke(tag_t<0>, Args&&... args) const + auto _invoke(std::false_type, Args&&... args) const { return [r_closure = Function(Function::operator()(std::forward(args)...))](auto&&... args) { return as(r_closure(std::forward(args)...)); }; } - template - auto _invoke(tag_t, Args&&... args) const + template + auto _invoke(std::true_type, Args&&... args) const { - return [r_call = RObject(Rf_lang(Function::operator()(std::forward(args)...), std::forward(args)...))](auto&&...) { + return [r_call = Shield(Rf_lang(Function::operator()(std::forward(args)...), std::forward(args)...))](auto&&...) { return as(Rcpp_fast_eval(r_call, R_GlobalEnv)); }; } @@ -59,8 +56,8 @@ SEXP twosample_pmt( const bool progress) { return progress ? - impl_twosample_pmt>(clone(x), clone(y), statistic_func, n_permu) : - impl_twosample_pmt>(clone(x), clone(y), statistic_func, n_permu); + impl_twosample_pmt>(clone(x), clone(y), statistic_func, n_permu) : + impl_twosample_pmt>(clone(x), clone(y), statistic_func, n_permu); } #include "pmt/impl_ksample_pmt.hpp" @@ -74,8 +71,8 @@ SEXP ksample_pmt( const bool progress) { return progress ? - impl_ksample_pmt>(data, clone(group), statistic_func, n_permu) : - impl_ksample_pmt>(data, clone(group), statistic_func, n_permu); + impl_ksample_pmt>(data, clone(group), statistic_func, n_permu) : + impl_ksample_pmt>(data, clone(group), statistic_func, n_permu); } #include "pmt/impl_multcomp_pmt.hpp" @@ -91,8 +88,8 @@ SEXP multcomp_pmt( const bool progress) { return progress ? - impl_multcomp_pmt>(group_i, group_j, data, clone(group), statistic_func, n_permu) : - impl_multcomp_pmt>(group_i, group_j, data, clone(group), statistic_func, n_permu); + impl_multcomp_pmt>(group_i, group_j, data, clone(group), statistic_func, n_permu) : + impl_multcomp_pmt>(group_i, group_j, data, clone(group), statistic_func, n_permu); } #include "pmt/impl_paired_pmt.hpp" @@ -106,8 +103,8 @@ SEXP paired_pmt( const bool progress) { return progress ? - impl_paired_pmt>(clone(x), clone(y), statistic_func, n_permu) : - impl_paired_pmt>(clone(x), clone(y), statistic_func, n_permu); + impl_paired_pmt>(clone(x), clone(y), statistic_func, n_permu) : + impl_paired_pmt>(clone(x), clone(y), statistic_func, n_permu); } #include "pmt/impl_rcbd_pmt.hpp" @@ -120,8 +117,8 @@ SEXP rcbd_pmt( const bool progress) { return progress ? - impl_rcbd_pmt>(clone(data), statistic_func, n_permu) : - impl_rcbd_pmt>(clone(data), statistic_func, n_permu); + impl_rcbd_pmt>(clone(data), statistic_func, n_permu) : + impl_rcbd_pmt>(clone(data), statistic_func, n_permu); } #include "pmt/impl_association_pmt.hpp" @@ -135,8 +132,8 @@ SEXP association_pmt( const bool progress) { return progress ? - impl_association_pmt>(clone(x), clone(y), statistic_func, n_permu) : - impl_association_pmt>(clone(x), clone(y), statistic_func, n_permu); + impl_association_pmt>(clone(x), clone(y), statistic_func, n_permu) : + impl_association_pmt>(clone(x), clone(y), statistic_func, n_permu); } #include "pmt/impl_table_pmt.hpp" @@ -150,6 +147,6 @@ SEXP table_pmt( const bool progress) { return progress ? - impl_table_pmt>(clone(row), clone(col), statistic_func, n_permu) : - impl_table_pmt>(clone(row), clone(col), statistic_func, n_permu); + impl_table_pmt>(clone(row), clone(col), statistic_func, n_permu) : + impl_table_pmt>(clone(row), clone(col), statistic_func, n_permu); } \ No newline at end of file