Skip to content

Commit

Permalink
Refactor. Added prevIterAvailable member. Added utilitly class functi…
Browse files Browse the repository at this point in the history
…ons.
  • Loading branch information
jwood000 committed Feb 11, 2022
1 parent c952f02 commit db7cce2
Show file tree
Hide file tree
Showing 19 changed files with 327 additions and 320 deletions.
4 changes: 4 additions & 0 deletions inst/include/ClassUtils/ComboClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class Combo {
const nextIterPtr nextIter;
const prevIterPtr prevIter;

bool prevIterAvailable;
SEXP ToSeeLast(bool AdjustIdx = true);
SEXP ToSeeFirst(bool AdjustIdx = true);

public:

Combo(
Expand Down
12 changes: 8 additions & 4 deletions inst/include/Combinations/ComboMultisetApply.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ void MultisetComboApplyFun(SEXP res, const std::vector<T> &v,
std::vector<int> zIndex(n);
const int retType = TYPEOF(res);

for (int i = 0; i < n; ++i)
zIndex[i] = std::find(freqs.cbegin(), freqs.cend(), i) - freqs.cbegin();
for (int i = 0; i < n; ++i) {
zIndex[i] = std::find(freqs.cbegin(),
freqs.cend(), i) - freqs.cbegin();
}

for (int count = 0, m1 = m - 1,
pentExtreme = freqs.size() - m; count < nRows;) {
Expand All @@ -42,8 +44,10 @@ void MultisetComboApplyFun(SEXP res, SEXP v, SEXP vectorPass,
std::vector<int> zIndex(n);
const int retType = TYPEOF(res);

for (int i = 0; i < n; ++i)
zIndex[i] = std::find(freqs.cbegin(), freqs.cend(), i) - freqs.cbegin();
for (int i = 0; i < n; ++i) {
zIndex[i] = std::find(freqs.cbegin(),
freqs.cend(), i) - freqs.cbegin();
}

for (int count = 0, m1 = m - 1,
pentExtreme = freqs.size() - m; count < nRows;) {
Expand Down
24 changes: 11 additions & 13 deletions src/CnstrntsSpecialClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CnstrntsSpecial::CnstrntsSpecial(
RcnstrtRowsMpz) {
count = 0;
keepGoing = true;
prevIterAvailable = false;
}

void CnstrntsSpecial::startOver() {
Expand All @@ -43,11 +44,11 @@ SEXP CnstrntsSpecial::nextComb() {
const std::string message = "No more results\n\n";
Rprintf(message.c_str());
UNPROTECT(1);
return Rf_ScalarLogical(false);
return R_NilValue;
}
}
} else {
return Rf_ScalarLogical(false);
return R_NilValue;
}
}

Expand All @@ -74,11 +75,11 @@ SEXP CnstrntsSpecial::nextNumCombs(SEXP RNum) {
const std::string message = "No more results\n\n";
Rprintf(message.c_str());
UNPROTECT(1);
return Rf_ScalarLogical(false);
return R_NilValue;
}
}
} else {
return Rf_ScalarLogical(false);
return R_NilValue;
}
}

Expand All @@ -90,7 +91,7 @@ SEXP CnstrntsSpecial::nextGather() {
UNPROTECT(1);
return res;
} else {
return Rf_ScalarLogical(false);
return R_NilValue;
}
}

Expand All @@ -99,8 +100,8 @@ SEXP CnstrntsSpecial::currComb() {
}

SEXP CnstrntsSpecial::summary() {
SEXP parent = PROTECT(Combo::summary());
std::string desc(R_CHAR(STRING_ELT(VECTOR_ELT(parent, 0), 0)));
SEXP res = PROTECT(Combo::summary());
std::string desc(R_CHAR(STRING_ELT(VECTOR_ELT(res, 0), 0)));

std::string val1 = (RTYPE == INTSXP) ?
std::to_string(tarIntVals.front()) :
Expand All @@ -124,14 +125,11 @@ SEXP CnstrntsSpecial::summary() {
desc += compVec.front() + " " + val1;
}

const char *names[] = {"description", "currentIndex",
"totalResultsWithoutConstraints", ""};

SEXP res = PROTECT(Rf_mkNamed(VECSXP, names));
SET_VECTOR_ELT(res, 0, Rf_mkString(desc.c_str()));
SET_VECTOR_ELT(res, 1, Rf_ScalarInteger(count));
SET_VECTOR_ELT(res, 2, VECTOR_ELT(parent, 2));
SET_VECTOR_ELT(res, 2, Rf_ScalarReal(R_NaReal));
SET_VECTOR_ELT(res, 3, Rf_ScalarReal(R_NaReal));

UNPROTECT(2);
UNPROTECT(1);
return res;
}
30 changes: 13 additions & 17 deletions src/CnstrntsToRClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ SEXP GetNextCnstrt(const std::vector<std::string> &compVec,
keepGoing = false;
const std::string message = "No more results.\n\n";
Rprintf(message.c_str());
return Rf_ScalarLogical(false);
return R_NilValue;
}
}

Expand Down Expand Up @@ -126,7 +126,7 @@ SEXP CnstrntsToR::GetNextN(int n) {
keepGoing = false;
const std::string message = "No more results.\n\n";
Rprintf(message.c_str());
return Rf_ScalarLogical(false);
return R_NilValue;
}

CnstrntsToR::CnstrntsToR(
Expand Down Expand Up @@ -167,6 +167,7 @@ CnstrntsToR::CnstrntsToR(
std::vector<int> intVec;
vecMax = std::floor(intVec.max_size() / m);
upperBoundInt = std::min(vecMax, dblIntMax);
prevIterAvailable = false;
}

void CnstrntsToR::startOver() {
Expand All @@ -188,7 +189,7 @@ SEXP CnstrntsToR::nextComb() {
if (keepGoing) {
return GetNext();
} else {
return Rf_ScalarLogical(false);
return R_NilValue;
}
}

Expand All @@ -201,7 +202,7 @@ SEXP CnstrntsToR::nextNumCombs(SEXP RNum) {
if (keepGoing) {
return GetNextN(num);
} else {
return Rf_ScalarLogical(false);
return R_NilValue;
}
}

Expand All @@ -212,29 +213,26 @@ SEXP CnstrntsToR::nextGather() {
maxRows - CnstrtDbl->GetCount();
return GetNextN(num);
} else {
return Rf_ScalarLogical(false);
return R_NilValue;
}
}

SEXP CnstrntsToR::currComb() {

if (!keepGoing) {
return Rf_ScalarLogical(0);
return R_NilValue;
} else if (RTYPE == INTSXP && CnstrtInt->GetCount()) {
return CnstrtVecReturn<INTSXP>(currIntVec);
} else if (RTYPE == REALSXP && CnstrtDbl->GetCount()) {
return CnstrtVecReturn<REALSXP>(currDblVec);
} else {
const std::string message = "Iterator Initialized. To see the first "
"result, use the nextIter method(s)\n\n";
Rprintf(message.c_str());
return Rf_ScalarLogical(0);
return ToSeeFirst(false);
}
}

SEXP CnstrntsToR::summary() {
SEXP parent = PROTECT(Combo::summary());
std::string desc(R_CHAR(STRING_ELT(VECTOR_ELT(parent, 0), 0)));
SEXP res = PROTECT(Combo::summary());
std::string desc(R_CHAR(STRING_ELT(VECTOR_ELT(res, 0), 0)));

std::string val1 = (RTYPE == INTSXP) ?
std::to_string(tarIntVals.front()) :
Expand All @@ -260,14 +258,12 @@ SEXP CnstrntsToR::summary() {

const int idx = (RTYPE == INTSXP) ? CnstrtInt->GetCount() :
CnstrtDbl->GetCount();
const char *names[] = {"description", "currentIndex",
"totalResultsWithoutConstraints", ""};

SEXP res = PROTECT(Rf_mkNamed(VECSXP, names));
SET_VECTOR_ELT(res, 0, Rf_mkString(desc.c_str()));
SET_VECTOR_ELT(res, 1, Rf_ScalarInteger(idx));
SET_VECTOR_ELT(res, 2, VECTOR_ELT(parent, 2));
SET_VECTOR_ELT(res, 2, Rf_ScalarReal(R_NaReal));
SET_VECTOR_ELT(res, 3, Rf_ScalarReal(R_NaReal));

UNPROTECT(2);
UNPROTECT(1);
return res;
}
28 changes: 8 additions & 20 deletions src/CombinatoricsCount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ SEXP PartitionsCount(SEXP Rtarget, SEXP Rv, SEXP Rm,
const std::string mainFun = "sum";
bool IsRep = CleanConvert::convertFlag(RisRep, "repetition");
const bool bDesign = CleanConvert::convertFlag(RPartDesign,
"PartitionsDesign");
"PartitionsDesign");

SetType(myType, Rv);
SetValues(myType, myReps, freqs, vInt, vNum, Rv,
Expand All @@ -85,8 +85,9 @@ SEXP PartitionsCount(SEXP Rtarget, SEXP Rv, SEXP Rm,

if (IsConstrained) {
ConstraintSetup(vNum, myReps, targetVals, vInt, targetIntVals,
funDbl, part, ctype, n, m, compVec, mainFun, mainFun,
myType, Rtarget, RcompFun, Rtolerance, Rlow, true, true);
funDbl, part, ctype, n, m, compVec, mainFun,
mainFun, myType, Rtarget, RcompFun, Rtolerance,
Rlow, true, true);
}

if (part.ptype != PartitionType::CoarseGrained &&
Expand All @@ -99,24 +100,11 @@ SEXP PartitionsCount(SEXP Rtarget, SEXP Rv, SEXP Rm,
return CleanConvert::GetCount(part.isGmp, part.bigCount,
part.count);
}
} else if (bDesign) {
Rf_error("No design available for this case!");
} else {
if (m == 1) {
const auto it = std::find(vNum.cbegin(), vNum.cend(),
targetVals.front());
if (it != vNum.cend()) {
return Rf_ScalarInteger(1);
} else {
return Rf_ScalarInteger(0);
}
} else if (n == 1) {
if (m == targetVals.front()) {
return Rf_ScalarInteger(1);
} else {
return Rf_ScalarInteger(0);
}
} else {
return Rf_ScalarInteger(0);
}
Rf_error("The count is unknown for this case. To get the total"
" number, generate all results!");
}
}

Expand Down
61 changes: 24 additions & 37 deletions src/ComboApplyClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,9 @@ SEXP ComboApply::nextComb() {
return VecApplyReturn();
} else if (CheckEqInd(IsGmp, mpzIndex, dblIndex,
computedRowsMpz, computedRows)) {
const std::string message = "No more results. To see the last "
"result, use the prevIter method(s)\n\n";
Rprintf(message.c_str());
increment(IsGmp, mpzIndex, dblIndex);
return Rf_ScalarLogical(false);
return ToSeeLast();
} else {
return Rf_ScalarLogical(false);
return R_NilValue;
}
}

Expand All @@ -120,13 +116,9 @@ SEXP ComboApply::prevComb() {
prevIter(freqs, z, n1, m1);
return VecApplyReturn();
} else if (CheckEqSi(IsGmp, mpzIndex, dblIndex, 1)) {
const std::string message = "Iterator Initialized. To see the first"
" result, use the nextIter method(s)\n\n";
Rprintf(message.c_str());
decrement(IsGmp, mpzIndex, dblIndex);
return Rf_ScalarLogical(false);
return ToSeeFirst();
} else {
return Rf_ScalarLogical(false);
return R_NilValue;
}
}

Expand Down Expand Up @@ -171,13 +163,9 @@ SEXP ComboApply::nextNumCombs(SEXP RNum) {
return res;
} else if (CheckEqInd(IsGmp, mpzIndex, dblIndex,
computedRowsMpz, computedRows)) {
const std::string message = "No more results. To see the last result"
", use the prevIter method(s)\n\n";
Rprintf(message.c_str());
increment(IsGmp, mpzIndex, dblIndex);
return Rf_ScalarLogical(false);
return ToSeeLast();
} else {
return Rf_ScalarLogical(false);
return R_NilValue;
}
}

Expand All @@ -187,7 +175,7 @@ SEXP ComboApply::prevNumCombs(SEXP RNum) {
CleanConvert::convertPrimitive(RNum, num, VecType::Integer,
"The number of results");

if (CheckGrTSi(IsGmp, mpzIndex, dblIndex, 2)) {
if (CheckGrTSi(IsGmp, mpzIndex, dblIndex, 1)) {
int nRows = 0;
int numDecrement = 0;

Expand All @@ -208,19 +196,20 @@ SEXP ComboApply::prevNumCombs(SEXP RNum) {

decrement(IsGmp, mpzIndex, dblIndex, numDecrement);
return ApplyReverse(nRows);
} else if (CheckEqSi(IsGmp, mpzIndex, dblIndex, 2)) {
const std::string message = "No more results. To see the last result"
", use the prevIter method(s)\n\n";
Rprintf(message.c_str());
decrement(IsGmp, mpzIndex, dblIndex);
return Rf_ScalarLogical(false);
} else if (CheckEqSi(IsGmp, mpzIndex, dblIndex, 1)) {
return ToSeeFirst();
} else {
return Rf_ScalarLogical(false);
return R_NilValue;
}
}

SEXP ComboApply::nextGather() {

if (CheckEqInd(IsGmp, mpzIndex, dblIndex,
computedRowsMpz, computedRows)) {
return ToSeeLast();
}

if (IsGmp) {
mpz_sub(mpzTemp, computedRowsMpz, mpzIndex);

Expand Down Expand Up @@ -262,12 +251,16 @@ SEXP ComboApply::nextGather() {
UNPROTECT(1);
return res;
} else {
return Rf_ScalarLogical(false);
return R_NilValue;
}
}

SEXP ComboApply::prevGather() {

if (CheckEqSi(IsGmp, mpzIndex, dblIndex, 1)) {
return ToSeeFirst();
}

if (IsGmp) {
mpz_sub_ui(mpzTemp, mpzIndex, 1);

Expand All @@ -286,7 +279,7 @@ SEXP ComboApply::prevGather() {

const int nRows = (IsGmp) ? mpz_get_si(mpzTemp) : dblTemp;

if (nRows) {
if (nRows > 0) {
if (CheckIndLT(IsGmp, mpzIndex, dblIndex, computedRowsMpz, computedRows, true))
prevIter(freqs, z, n1, m1);

Expand All @@ -298,25 +291,19 @@ SEXP ComboApply::prevGather() {

return ApplyReverse(nRows);
} else {
return Rf_ScalarLogical(false);
return R_NilValue;
}
}

SEXP ComboApply::currComb() {

if (CheckIndGrT(IsGmp, mpzIndex, dblIndex,
computedRowsMpz, computedRows)) {
const std::string message = "No more results. To see the last "
"result, use the prevIter method(s)\n\n";
Rprintf(message.c_str());
return Rf_ScalarLogical(0);
return ToSeeLast(false);
} else if (CheckGrTSi(IsGmp, mpzIndex, dblIndex, 0)) {
return VecApplyReturn();
} else {
const std::string message = "Iterator Initialized. To see the first "
"result, use the nextIter method(s)\n\n";
Rprintf(message.c_str());
return Rf_ScalarLogical(0);
return ToSeeFirst(false);
}
}

Expand Down
Loading

0 comments on commit db7cce2

Please sign in to comment.