Skip to content

Commit

Permalink
overly protective ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
mattfidler committed Sep 16, 2024
1 parent adcffa0 commit 5ab3b7d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 25 deletions.
44 changes: 33 additions & 11 deletions src/et.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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<std::vector<int>>(ord);
List newEt(curEt.size());
Expand Down Expand Up @@ -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<std::vector<int>>(ord);
List lst(curEt.size());
Expand Down Expand Up @@ -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<std::vector<int>>(ord);
List lst(curEt.size());
Expand Down Expand Up @@ -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<std::vector<int>>(ord);

Expand Down Expand Up @@ -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<std::vector<int>>(ord);
}
Expand Down
34 changes: 25 additions & 9 deletions src/etTran.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -2152,28 +2156,40 @@ 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<IntegerVector>(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<std::vector<int>>(ord);
while (idxOutput.size() > 0 && IntegerVector::is_na(ivId[idxOutput.back()])){
idxOutput.pop_back();
}
if (hasIcov) {
ordI = order(inIdCov, _["na.last"] = LogicalVector::create(true));
SEXP inIdCovSxp = PROTECT(wrap(inIdCov));
ordI = as<IntegerVector>(order(inIdCovSxp, _["na.last"] = LogicalVector::create(true)));
UNPROTECT(1);
ordI = ordI-1;
idxIcov = as<std::vector<int>>(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<IntegerVector>(order(ivIdSxp, nvTimeSxp, ivEvidSxp,
_["na.last"] = LogicalVector::create(NA_LOGICAL),
_["method"]="radix"));
UNPROTECT(3);
ord = ord - 1;
idxOutput = as<std::vector<int>>(ord);
if (hasIcov) {
ordI = order(inIdCov, _["na.last"] = LogicalVector::create(true),
_["method"]="radix");
SEXP inIdCovSxp = PROTECT(wrap(inIdCov));
ordI = as<IntegerVector>(order(inIdCovSxp, _["na.last"] = LogicalVector::create(true),
_["method"]="radix"));
UNPROTECT(1);
ordI = ordI-1;
idxIcov = as<std::vector<int>>(ordI);
}
Expand Down
14 changes: 9 additions & 5 deletions src/rxData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2841,14 +2841,18 @@ extern "C" void sortIds(rx_solve* rx, int ini) {
solveTime[i] = ind->solveTime;
}
Function order = as<Function>(getForder());
SEXP solveTimeSEXP = PROTECT(wrap(solveTime));
if (useForder()) {
ord = order(solveTime, _["na.last"] = LogicalVector::create(NA_LOGICAL),
_["decreasing"] = LogicalVector::create(true));
ord = as<IntegerVector>(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<IntegerVector>(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);
}
Expand Down

0 comments on commit 5ab3b7d

Please sign in to comment.