Skip to content

Commit

Permalink
use sizeof...
Browse files Browse the repository at this point in the history
  • Loading branch information
qddyy committed Dec 18, 2024
1 parent a3e1ed2 commit d8eac7a
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions src/pmt_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ using namespace Rcpp;
#include "pmt/permutation.hpp"
#include "pmt/progress.hpp"

#include <cstddef>
#include <type_traits>

template <unsigned i>
using tag_t = std::integral_constant<unsigned, i>;

template <unsigned n>
template <std::size_t n>
constexpr auto Rf_lang = nullptr;

template <>
Expand All @@ -19,30 +17,30 @@ constexpr auto Rf_lang<2> = Rf_lang2;
template <>
constexpr auto Rf_lang<3> = Rf_lang3;

template <unsigned n_shared>
template <bool sharing_args>
class StatFunc : public Function {
public:
using Function::Function;

template <typename... Args>
auto operator()(Args&&... args) const
{
return _invoke(tag_t<n_shared>(), std::forward<Args>(args)...);
return _invoke(std::integral_constant<bool, sharing_args>(), std::forward<Args>(args)...);
}

private:
template <typename... Args>
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>(args)...))](auto&&... args) {
return as<double>(r_closure(std::forward<decltype(args)>(args)...));
};
}

template <unsigned n = n_shared, typename... Args>
auto _invoke(tag_t<n>, Args&&... args) const
template <typename... Args>
auto _invoke(std::true_type, Args&&... args) const
{
return [r_call = RObject(Rf_lang<n + 1>(Function::operator()(std::forward<Args>(args)...), std::forward<Args>(args)...))](auto&&...) {
return [r_call = RObject(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));
};
}
Expand All @@ -59,8 +57,8 @@ SEXP twosample_pmt(
const bool progress)
{
return progress ?
impl_twosample_pmt<true, StatFunc<2>>(clone(x), clone(y), statistic_func, n_permu) :
impl_twosample_pmt<false, StatFunc<2>>(clone(x), clone(y), statistic_func, n_permu);
impl_twosample_pmt<true, StatFunc<true>>(clone(x), clone(y), statistic_func, n_permu) :
impl_twosample_pmt<false, StatFunc<true>>(clone(x), clone(y), statistic_func, n_permu);
}

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

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

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

#include "pmt/impl_rcbd_pmt.hpp"
Expand All @@ -120,8 +118,8 @@ SEXP rcbd_pmt(
const bool progress)
{
return progress ?
impl_rcbd_pmt<true, StatFunc<1>>(clone(data), statistic_func, n_permu) :
impl_rcbd_pmt<false, StatFunc<1>>(clone(data), statistic_func, n_permu);
impl_rcbd_pmt<true, StatFunc<true>>(clone(data), statistic_func, n_permu) :
impl_rcbd_pmt<false, StatFunc<true>>(clone(data), statistic_func, n_permu);
}

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

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

0 comments on commit d8eac7a

Please sign in to comment.