@@ -11,35 +11,29 @@ constexpr auto Rf_lang<2> = Rf_lang2;
1111template <>
1212constexpr auto Rf_lang<3 > = Rf_lang3;
1313
14- template <bool sharing_args >
15- class StatFunc : public Function {
14+ template <typename T >
15+ class CachedFunc : public Function {
1616public:
1717 using Function::Function;
1818
1919 template <typename ... Args>
2020 auto operator ()(Args&&... args) const
2121 {
22- return _invoke (Tag<sharing_args>(), std::forward<Args>(args)...);
23- }
24-
25- private:
26- template <bool >
27- struct Tag { };
28-
29- template <typename ... Args>
30- auto _invoke (Tag<false >, Args&&... args) const
31- {
32- return [R_closure = Function (Function::operator ()(std::forward<Args>(args)...))](auto &&... args) {
33- return as<double >(R_closure (std::forward<decltype (args)>(args)...));
22+ return [R_call = Shield<SEXP>(Rf_lang<sizeof ...(args) + 1 >(Function::operator ()(std::forward<Args>(args)...), std::forward<Args>(args)...))](auto &&...) {
23+ return as<T>(Rcpp_fast_eval (R_call, R_GlobalEnv));
3424 };
3525 }
26+ };
27+
28+ template <typename T>
29+ class TypedFunc : public Function {
30+ public:
31+ using Function::Function;
3632
3733 template <typename ... Args>
38- auto _invoke (Tag< true >, Args&&... args) const
34+ T operator ()( Args&&... args) const
3935 {
40- return [R_call = Shield<SEXP>(Rf_lang<sizeof ...(args) + 1 >(Function::operator ()(std::forward<Args>(args)...), std::forward<Args>(args)...))](auto &&...) {
41- return as<double >(Rcpp_fast_eval (R_call, R_GlobalEnv));
42- };
36+ return as<T>(Function::operator ()(std::forward<Args>(args)...));
4337 }
4438};
4539
@@ -57,8 +51,8 @@ SEXP twosample_pmt(
5751 const bool progress)
5852{
5953 return progress ?
60- impl_twosample_pmt<true , StatFunc< true >>(clone (x), clone (y), statistic_func, n_permu) :
61- impl_twosample_pmt<false , StatFunc< true >>(clone (x), clone (y), statistic_func, n_permu);
54+ impl_twosample_pmt<true , CachedFunc< double >>(clone (x), clone (y), statistic_func, n_permu) :
55+ impl_twosample_pmt<false , CachedFunc< double >>(clone (x), clone (y), statistic_func, n_permu);
6256}
6357
6458#include " pmt/impl_ksample_pmt.hpp"
@@ -72,25 +66,23 @@ SEXP ksample_pmt(
7266 const bool progress)
7367{
7468 return progress ?
75- impl_ksample_pmt<true , StatFunc< true >>(data, clone (group), statistic_func, n_permu) :
76- impl_ksample_pmt<false , StatFunc< true >>(data, clone (group), statistic_func, n_permu);
69+ impl_ksample_pmt<true , CachedFunc< double >>(data, clone (group), statistic_func, n_permu) :
70+ impl_ksample_pmt<false , CachedFunc< double >>(data, clone (group), statistic_func, n_permu);
7771}
7872
7973#include " pmt/impl_multcomp_pmt.hpp"
8074
8175// [[Rcpp::export]]
8276SEXP multcomp_pmt (
83- const SEXP group_i,
84- const SEXP group_j,
8577 const SEXP data,
8678 const SEXP group,
8779 const SEXP statistic_func,
8880 const double n_permu,
8981 const bool progress)
9082{
9183 return progress ?
92- impl_multcomp_pmt<true , StatFunc< false >>(group_i, group_j, data, clone (group), statistic_func, n_permu) :
93- impl_multcomp_pmt<false , StatFunc< false >>(group_i, group_j, data, clone (group), statistic_func, n_permu);
84+ impl_multcomp_pmt<true , CachedFunc<TypedFunc< double >>>( data, clone (group), statistic_func, n_permu) :
85+ impl_multcomp_pmt<false , CachedFunc<TypedFunc< double >>>( data, clone (group), statistic_func, n_permu);
9486}
9587
9688#include " pmt/impl_paired_pmt.hpp"
@@ -104,8 +96,8 @@ SEXP paired_pmt(
10496 const bool progress)
10597{
10698 return progress ?
107- impl_paired_pmt<true , StatFunc< true >>(clone (x), clone (y), statistic_func, n_permu) :
108- impl_paired_pmt<false , StatFunc< true >>(clone (x), clone (y), statistic_func, n_permu);
99+ impl_paired_pmt<true , CachedFunc< double >>(clone (x), clone (y), statistic_func, n_permu) :
100+ impl_paired_pmt<false , CachedFunc< double >>(clone (x), clone (y), statistic_func, n_permu);
109101}
110102
111103#include " pmt/impl_rcbd_pmt.hpp"
@@ -118,8 +110,8 @@ SEXP rcbd_pmt(
118110 const bool progress)
119111{
120112 return progress ?
121- impl_rcbd_pmt<true , StatFunc< true >>(clone (data), statistic_func, n_permu) :
122- impl_rcbd_pmt<false , StatFunc< true >>(clone (data), statistic_func, n_permu);
113+ impl_rcbd_pmt<true , CachedFunc< double >>(clone (data), statistic_func, n_permu) :
114+ impl_rcbd_pmt<false , CachedFunc< double >>(clone (data), statistic_func, n_permu);
123115}
124116
125117#include " pmt/impl_association_pmt.hpp"
@@ -133,8 +125,8 @@ SEXP association_pmt(
133125 const bool progress)
134126{
135127 return progress ?
136- impl_association_pmt<true , StatFunc< true >>(clone (x), clone (y), statistic_func, n_permu) :
137- impl_association_pmt<false , StatFunc< true >>(clone (x), clone (y), statistic_func, n_permu);
128+ impl_association_pmt<true , CachedFunc< double >>(clone (x), clone (y), statistic_func, n_permu) :
129+ impl_association_pmt<false , CachedFunc< double >>(clone (x), clone (y), statistic_func, n_permu);
138130}
139131
140132#include " pmt/impl_table_pmt.hpp"
@@ -148,6 +140,6 @@ SEXP table_pmt(
148140 const bool progress)
149141{
150142 return progress ?
151- impl_table_pmt<true , StatFunc< true >>(clone (row), clone (col), statistic_func, n_permu) :
152- impl_table_pmt<false , StatFunc< true >>(clone (row), clone (col), statistic_func, n_permu);
143+ impl_table_pmt<true , CachedFunc< double >>(clone (row), clone (col), statistic_func, n_permu) :
144+ impl_table_pmt<false , CachedFunc< double >>(clone (row), clone (col), statistic_func, n_permu);
153145}
0 commit comments