From 8ba89f5b3b3421b366e6a203d920b54ce87403d2 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Mon, 29 Apr 2024 12:45:06 -0700 Subject: [PATCH] use R_xlen_t in loops --- src/altrep-sparse-real.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/altrep-sparse-real.c b/src/altrep-sparse-real.c index 3d55ab7..5972ced 100644 --- a/src/altrep-sparse-real.c +++ b/src/altrep-sparse-real.c @@ -66,11 +66,13 @@ static SEXP altrep_sparse_real_Extract_subset(SEXP x, SEXP indx, SEXP call) { SEXP data1 = R_altrep_data1(x); SEXP val_old = VECTOR_ELT(data1, 0); SEXP pos_old = VECTOR_ELT(data1, 1); + SEXP matches = PROTECT(Rf_match(pos_old, indx, R_NaInt)); + R_xlen_t n_matches = Rf_xlength(matches); int n = 0; - for (int i = 0; i < Rf_length(matches); ++i) { + for (R_xlen_t i = 0; i < n_matches; ++i) { if (INTEGER_ELT(matches, i) != R_NaInt) { n++; } @@ -82,13 +84,13 @@ static SEXP altrep_sparse_real_Extract_subset(SEXP x, SEXP indx, SEXP call) { int step = 0; int what_pos = 1; - for (int i = 0; i < Rf_length(matches); ++i) { + for (R_xlen_t i = 0; i < n_matches; ++i) { int match = INTEGER_ELT(matches, i); if (match != R_NaInt) { SET_REAL_ELT(val_new, step, REAL_ELT(val_old, match - 1)); - for (int j = 0; j < Rf_length(matches); ++j) { + for (R_xlen_t j = 0; j < n_matches; ++j) { if (INTEGER_ELT(indx, j) == INTEGER_ELT(pos_old, match - 1)) { break; } else { @@ -151,7 +153,7 @@ static double altrep_sparse_real_Elt(SEXP x, R_xlen_t i) { double out = 0; - for (int j = 0; j < n; ++j) { + for (R_xlen_t j = 0; j < n; ++j) { if (INTEGER_ELT(pos, j) == i + 1) { out = REAL_ELT(val, j); break;