Skip to content

Commit

Permalink
Added struct for easier handling of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jwood000 committed Sep 27, 2023
1 parent cbd164a commit faf28fc
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 67 deletions.
7 changes: 7 additions & 0 deletions inst/include/ComboGroups/ComboGroupsTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ typedef std::function<void(
const std::vector<mpz_class>&, bool
)> finalTouchFunc;

struct CmbGrpClsFuncs {
const nthFuncDbl nthDbl;
const nthFuncGmp nthGmp;
const nextGrpFunc next;
const finalTouchFunc finishing;
};

class ComboGroupsTemplate {
protected:

Expand Down
4 changes: 4 additions & 0 deletions inst/include/ComboGroups/GetComboGroups.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include <numeric>
#include <thread>

CmbGrpClsFuncs GetClassFuncs(
std::unique_ptr<ComboGroupsTemplate> const &CmbGrp
);

SEXP GetComboGroups(
SEXP Rv, nextGrpFunc nextCmbGrp, nthFuncDbl nthCmbGrp,
nthFuncGmp nthCmbGrpGmp, finalTouchFunc FinalTouch,
Expand Down
24 changes: 2 additions & 22 deletions src/ComboGroups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,10 @@ SEXP ComboGroupsCpp(SEXP Rv, SEXP RNumGroups, SEXP RGrpSize, SEXP RRetType,
SetThreads(Parallel, maxThreads, numResults,
myType, nThreads, RNumThreads, limit);

const nextGrpFunc nextCmbGrp = std::bind(
&ComboGroupsTemplate::nextComboGroup,
std::cref(CmbGrp), std::placeholders::_1
);

const nthFuncDbl nthCmbGrp = std::bind(
&ComboGroupsTemplate::nthComboGroup,
std::cref(CmbGrp), std::placeholders::_1
);

const nthFuncGmp nthCmbGrpGmp = std::bind(
&ComboGroupsTemplate::nthComboGroupGmp,
std::cref(CmbGrp), std::placeholders::_1
);

const finalTouchFunc FinalTouch = std::bind(
&ComboGroupsTemplate::FinalTouch, std::cref(CmbGrp),
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
std::placeholders::_4, std::placeholders::_5, std::placeholders::_6,
std::placeholders::_7
);
CmbGrpClsFuncs f = GetClassFuncs(CmbGrp);

cpp11::sexp res = GetComboGroups(
Rv, nextCmbGrp, nthCmbGrp, nthCmbGrpGmp, FinalTouch, vNum, vInt,
Rv, f.next, f.nthDbl, f.nthGmp, f.finishing, vNum, vInt,
startZ, myType, mySample, myBigSamp, lowerMpz, lower, n, numResults,
nThreads, IsArray, IsNamed, Parallel, IsSample, IsGmp
);
Expand Down
49 changes: 4 additions & 45 deletions src/ComboGroupsClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,10 @@ SEXP ComboGroupsClass::GeneralReturn(int numResults) {
SetThreads(LocalPar, maxThreads, numResults,
myType, nThreads, sexpNThreads, limit);

const nextGrpFunc nextCmbGrp = std::bind(
&ComboGroupsTemplate::nextComboGroup,
std::cref(CmbGrp), std::placeholders::_1
);

const nthFuncDbl nthCmbGrp = std::bind(
&ComboGroupsTemplate::nthComboGroup,
std::cref(CmbGrp), std::placeholders::_1
);

const nthFuncGmp nthCmbGrpGmp = std::bind(
&ComboGroupsTemplate::nthComboGroupGmp,
std::cref(CmbGrp), std::placeholders::_1
);

const finalTouchFunc FinalTouch = std::bind(
&ComboGroupsTemplate::FinalTouch, std::cref(CmbGrp),
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
std::placeholders::_4, std::placeholders::_5, std::placeholders::_6,
std::placeholders::_7
);
CmbGrpClsFuncs f = GetClassFuncs(CmbGrp);

cpp11::sexp res = GetComboGroups(
sexpVec, nextCmbGrp, nthCmbGrp, nthCmbGrpGmp, FinalTouch, vNum,
sexpVec, f.next, f.nthDbl, f.nthGmp, f.finishing, vNum,
vInt, z, myType, tempSample, tempBigSamp, mpzIndex, dblIndex, n,
numResults, nThreads, IsArray, false, LocalPar, false, IsGmp
);
Expand Down Expand Up @@ -271,31 +251,10 @@ SEXP ComboGroupsClass::randomAccess(SEXP RindexVec) {
myType, nThreads, sexpNThreads, limit);

const std::vector<int> before(z);

const nextGrpFunc nextCmbGrp = std::bind(
&ComboGroupsTemplate::nextComboGroup,
std::cref(CmbGrp), std::placeholders::_1
);

const nthFuncDbl nthCmbGrp = std::bind(
&ComboGroupsTemplate::nthComboGroup,
std::cref(CmbGrp), std::placeholders::_1
);

const nthFuncGmp nthCmbGrpGmp = std::bind(
&ComboGroupsTemplate::nthComboGroupGmp,
std::cref(CmbGrp), std::placeholders::_1
);

const finalTouchFunc FinalTouch = std::bind(
&ComboGroupsTemplate::FinalTouch, std::cref(CmbGrp),
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
std::placeholders::_4, std::placeholders::_5, std::placeholders::_6,
std::placeholders::_7
);
CmbGrpClsFuncs f = GetClassFuncs(CmbGrp);

cpp11::sexp res = GetComboGroups(
sexpVec, nextCmbGrp, nthCmbGrp, nthCmbGrpGmp, FinalTouch, vNum,
sexpVec, f.next, f.nthDbl, f.nthGmp, f.finishing, vNum,
vInt, z, myType, mySample, mpzVec, mpzIndex, dblIndex, n,
sampSize, nThreads, IsArray, false, LocalPar, true, IsGmp
);
Expand Down
36 changes: 36 additions & 0 deletions src/GetComboGroups.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
#include "ComboGroups/GetComboGroups.h"

CmbGrpClsFuncs GetClassFuncs(
std::unique_ptr<ComboGroupsTemplate> const &CmbGrp
) {

const nextGrpFunc nextCmbGrp = std::bind(
&ComboGroupsTemplate::nextComboGroup,
std::cref(CmbGrp), std::placeholders::_1
);

const nthFuncDbl nthCmbGrp = std::bind(
&ComboGroupsTemplate::nthComboGroup,
std::cref(CmbGrp), std::placeholders::_1
);

const nthFuncGmp nthCmbGrpGmp = std::bind(
&ComboGroupsTemplate::nthComboGroupGmp,
std::cref(CmbGrp), std::placeholders::_1
);

const finalTouchFunc FinalTouch = std::bind(
&ComboGroupsTemplate::FinalTouch, std::cref(CmbGrp),
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
std::placeholders::_4, std::placeholders::_5, std::placeholders::_6,
std::placeholders::_7
);

CmbGrpClsFuncs myFuncs = {
nthCmbGrp,
nthCmbGrpGmp,
nextCmbGrp,
FinalTouch
};

return myFuncs;
}

void SampleResults(SEXP GroupsMat, SEXP v,
nthFuncDbl nthCmbGrp, nthFuncGmp nthCmbGrpGmp,
const std::vector<double> &mySample,
Expand Down

0 comments on commit faf28fc

Please sign in to comment.