Skip to content

Commit

Permalink
perf: pass numeric by reference in assert_surv
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muecke committed Aug 23, 2024
1 parent 737498e commit 7f6354e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// c_assert_surv
bool c_assert_surv(NumericMatrix mat);
bool c_assert_surv(const NumericMatrix& mat);
RcppExport SEXP _mlr3proba_c_assert_surv(SEXP matSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< NumericMatrix >::type mat(matSEXP);
Rcpp::traits::input_parameter< const NumericMatrix& >::type mat(matSEXP);
rcpp_result_gen = Rcpp::wrap(c_assert_surv(mat));
return rcpp_result_gen;
END_RCPP
Expand Down
33 changes: 16 additions & 17 deletions src/survival_assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@
using namespace Rcpp;

// [[Rcpp::export]]
bool c_assert_surv(NumericMatrix mat) {
for (int i = 0; i < mat.nrow(); i++) {
// check first element
if (mat(i, 0) < 0 || mat(i, 0) > 1) {
return false;
}
bool c_assert_surv(const NumericMatrix& mat) {
for (int i = 0; i < mat.nrow(); i++) {
if (mat(i, 0) < 0 || mat(i, 0) > 1) {
// check first element
return false;
}

for (int j = 1; j < mat.ncol(); j++) {
// S(t) in [0,1]
if (mat(i, j) < 0 || mat(i, j) > 1) {
return false;
}
for (int j = 1; j < mat.ncol(); j++) {
// S(t) in [0,1]
if (mat(i, j) < 0 || mat(i, j) > 1) {
return false;
}

// S(t) should not increase!
if (mat(i, j) > mat(i, j - 1)) {
return false;
}
// S(t) should not increase!
if (mat(i, j) > mat(i, j - 1)) {
return false;
}
}
}
}

return true;
}

0 comments on commit 7f6354e

Please sign in to comment.