From 5ab3b7d5b32a10797eae6920180fadfe3f3de2b7 Mon Sep 17 00:00:00 2001 From: Matthew Fidler Date: Mon, 16 Sep 2024 16:10:37 -0500 Subject: [PATCH] overly protective ordering --- src/et.cpp | 44 +++++++++++++++++++++++++++++++++----------- src/etTran.cpp | 34 +++++++++++++++++++++++++--------- src/rxData.cpp | 14 +++++++++----- 3 files changed, 67 insertions(+), 25 deletions(-) diff --git a/src/et.cpp b/src/et.cpp index f00ebe34d..69aa7e7e2 100644 --- a/src/et.cpp +++ b/src/et.cpp @@ -52,14 +52,16 @@ extern "C" SEXP orderForderS1(SEXP ordIn) { BEGIN_RCPP Function order = getForder(); IntegerVector ord; + SEXP ordInSexp = PROTECT(wrap(ordIn)); if (useForder()){ - ord = order(ordIn, + ord = order(ordInSexp, _["na.last"] = LogicalVector::create(0)); } else { - ord = order(ordIn, + ord = order(ordInSexp, _["na.last"] = LogicalVector::create(0), _["method"]="radix"); } + UNPROTECT(1); return wrap(ord); END_RCPP } @@ -432,14 +434,18 @@ List etSort(List& curEt){ IntegerVector ivEvid=wrap(evid); Function order = getForder(); IntegerVector ord; + SEXP ivIdSxp = PROTECT(wrap(ivId)); + SEXP nvTimeSxp = PROTECT(wrap(nvTime)); + SEXP ivEvidSxp = PROTECT(wrap(ivEvid)); if (useForder()){ - ord = order(ivId, nvTime, ivEvid, + ord = order(ivIdSxp, nvTimeSxp, ivEvidSxp, _["na.last"] = LogicalVector::create(0)); } else { ord = order(ivId, nvTime, ivEvid, _["na.last"] = LogicalVector::create(0), _["method"]="radix"); } + UNPROTECT(3); ord = ord - 1; idx = as>(ord); List newEt(curEt.size()); @@ -593,15 +599,19 @@ List etAddWindow(List windowLst, IntegerVector IDs, RObject cmt, bool turnOnShow NumericVector nvTime=wrap(time); IntegerVector ivEvid=wrap(evid); Function order = getForder(); + SEXP ivIdSxp = PROTECT(wrap(ivId)); + SEXP nvTimeSxp = PROTECT(wrap(nvTime)); + SEXP ivEvidSxp = PROTECT(wrap(ivEvid)); IntegerVector ord; if (useForder()){ - ord = order(ivId, nvTime, ivEvid, + ord = order(ivIdSxp, nvTimeSxp, ivEvidSxp, _["na.last"] = LogicalVector::create(NA_LOGICAL)); } else { - ord = order(ivId, nvTime, ivEvid, + ord = order(ivIdSxp, nvTimeSxp, ivEvidSxp, _["na.last"] = LogicalVector::create(NA_LOGICAL), _["method"]="radix"); } + UNPROTECT(3); ord = ord - 1; idx = as>(ord); List lst(curEt.size()); @@ -1533,14 +1543,18 @@ List etExpandAddl(List curEt){ IntegerVector ivEvid=wrap(evid); Function order = getForder(); IntegerVector ord; + SEXP ivIdSxp = PROTECT(wrap(ivId)); + SEXP nvTimeSxp = PROTECT(wrap(nvTime)); + SEXP ivEvidSxp = PROTECT(wrap(ivEvid)); if (useForder()){ - ord = order(ivId, nvTime, ivEvid, + ord = order(ivIdSxp, nvTimeSxp, ivEvidSxp, _["na.last"] = LogicalVector::create(NA_LOGICAL)); } else { - ord = order(ivId, nvTime, ivEvid, + ord = order(ivIdSxp, nvTimeSxp, ivEvidSxp, _["na.last"] = LogicalVector::create(NA_LOGICAL), _["method"]="radix"); } + UNPROTECT(3); ord = ord - 1; idx = as>(ord); List lst(curEt.size()); @@ -1715,14 +1729,18 @@ List etAddDose(NumericVector curTime, RObject cmt, double amt, double rate, dou IntegerVector ivEvid=wrap(evid); Function order = getForder(); IntegerVector ord; + SEXP ivIdSxp = PROTECT(wrap(ivId)); + SEXP nvTimeSxp = PROTECT(wrap(nvTime)); + SEXP ivEvidSxp = PROTECT(wrap(ivEvid)); if (useForder()){ - ord = order(ivId, nvTime, ivEvid, + ord = order(ivIdSxp, nvTimeSxp, ivEvidSxp, _["na.last"] = LogicalVector::create(NA_LOGICAL)); } else { - ord = order(ivId, nvTime, ivEvid, + ord = order(ivIdSxp, nvTimeSxp, ivEvidSxp, _["na.last"] = LogicalVector::create(NA_LOGICAL), _["method"]="radix"); } + UNPROTECT(3); ord = ord - 1; idx = as>(ord); @@ -3388,14 +3406,18 @@ List etSeq_(List ets, int handleSamples=0, int waitType = 0, IntegerVector ivEvid=wrap(evid); Function order = getForder(); IntegerVector ord; + SEXP ivIdSxp = PROTECT(wrap(ivId)); + SEXP nvTimeSxp = PROTECT(wrap(nvTime)); + SEXP ivEvidSxp = PROTECT(wrap(ivEvid)); if (useForder()){ - ord = order(ivId, nvTime, ivEvid, + ord = order(ivIdSxp, nvTimeSxp, ivEvidSxp, _["na.last"] = LogicalVector::create(NA_LOGICAL)); } else { - ord = order(ivId, nvTime, ivEvid, + ord = order(ivIdSxp, nvTimeSxp, ivEvidSxp, _["na.last"] = LogicalVector::create(NA_LOGICAL), _["method"]="radix"); } + UNPROTECT(3); ord = ord - 1; idx = as>(ord); } diff --git a/src/etTran.cpp b/src/etTran.cpp index aa2c3e4a6..f0e694a45 100644 --- a/src/etTran.cpp +++ b/src/etTran.cpp @@ -186,7 +186,11 @@ Function getChin() { extern "C" SEXP chin(SEXP x, SEXP table) { Function chin_ = getChin(); - return chin_(x, table); + SEXP xin = PROTECT(x); + SEXP tablein = PROTECT(table); + SEXP chinout = PROTECT(chin_(x, table)); + UNPROTECT(3); + return chinout; } extern "C" int useForder(void){ @@ -2152,8 +2156,12 @@ List etTrans(List inData, const RObject &obj, bool addCmt=false, IntegerVector ord; IntegerVector ordI; if (useForder()){ - ord = order(ivId, nvTime, ivEvid, - _["na.last"] = LogicalVector::create(true)); + SEXP ivIdSxp = PROTECT(wrap(ivId)); + SEXP nvTimeSxp = PROTECT(wrap(nvTime)); + SEXP ivEvidSxp = PROTECT(wrap(ivEvid)); + ord = as(order(ivIdSxp, nvTimeSxp, ivEvidSxp, + _["na.last"] = LogicalVector::create(true))); + UNPROTECT(3); ord = ord - 1; // na.last isn't =NA isn't quite working idxOutput = as>(ord); @@ -2161,19 +2169,27 @@ List etTrans(List inData, const RObject &obj, bool addCmt=false, idxOutput.pop_back(); } if (hasIcov) { - ordI = order(inIdCov, _["na.last"] = LogicalVector::create(true)); + SEXP inIdCovSxp = PROTECT(wrap(inIdCov)); + ordI = as(order(inIdCovSxp, _["na.last"] = LogicalVector::create(true))); + UNPROTECT(1); ordI = ordI-1; idxIcov = as>(ordI); } } else { - ord = order(ivId, nvTime, ivEvid, - _["na.last"] = LogicalVector::create(NA_LOGICAL), - _["method"]="radix"); + SEXP ivIdSxp = PROTECT(wrap(ivId)); + SEXP nvTimeSxp = PROTECT(wrap(nvTime)); + SEXP ivEvidSxp = PROTECT(wrap(ivEvid)); + ord = as(order(ivIdSxp, nvTimeSxp, ivEvidSxp, + _["na.last"] = LogicalVector::create(NA_LOGICAL), + _["method"]="radix")); + UNPROTECT(3); ord = ord - 1; idxOutput = as>(ord); if (hasIcov) { - ordI = order(inIdCov, _["na.last"] = LogicalVector::create(true), - _["method"]="radix"); + SEXP inIdCovSxp = PROTECT(wrap(inIdCov)); + ordI = as(order(inIdCovSxp, _["na.last"] = LogicalVector::create(true), + _["method"]="radix")); + UNPROTECT(1); ordI = ordI-1; idxIcov = as>(ordI); } diff --git a/src/rxData.cpp b/src/rxData.cpp index 10d68a989..7d7bc5a2b 100644 --- a/src/rxData.cpp +++ b/src/rxData.cpp @@ -2841,14 +2841,18 @@ extern "C" void sortIds(rx_solve* rx, int ini) { solveTime[i] = ind->solveTime; } Function order = as(getForder()); + SEXP solveTimeSEXP = PROTECT(wrap(solveTime)); if (useForder()) { - ord = order(solveTime, _["na.last"] = LogicalVector::create(NA_LOGICAL), - _["decreasing"] = LogicalVector::create(true)); + ord = as(order(solveTimeSEXP, + _["na.last"] = LogicalVector::create(NA_LOGICAL), + _["decreasing"] = LogicalVector::create(true))); } else { - ord = order(solveTime, _["na.last"] = LogicalVector::create(NA_LOGICAL), - _["method"]="radix", - _["decreasing"] = LogicalVector::create(true)); + ord = as(order(solveTimeSEXP, + _["na.last"] = LogicalVector::create(NA_LOGICAL), + _["method"]="radix", + _["decreasing"] = LogicalVector::create(true))); } + UNPROTECT(1); // This assumes that this has already been created std::copy(ord.begin(), ord.end(), rx->ordId); }