Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting removal of STRICT_R_HEADERS in 4.5.0 #272

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: matrixStats
Version: 1.4.1-9001
Version: 1.4.1-9002
Depends:
R (>= 2.12.0)
R (>= 3.4.0)
Suggests:
utils,
base64enc,
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Version (development version)

* ...
* Minimum version requirement lifted to 3.4.0 due to
removal of the flag `STRICT_R_HEADERS` in R 4.5.0.


# Version 1.4.1 [2024-09-06]
Expand Down
34 changes: 3 additions & 31 deletions src/000.types.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <Rinternals.h> /* R_xlen_t, ... */
#include <Rversion.h> /* R_VERSION, R_Version() */

/* Needed if compiled prior to 4.5.0 with STRICT_R_HEADERS */
#include <float.h>

#ifndef R_INT_MIN
#define R_INT_MIN -INT_MAX
#endif
Expand Down Expand Up @@ -35,37 +38,6 @@
#endif



/* Backward compatibility with R (< 3.0.0)
As in <R>/src/include/Rinternals.h */
#ifndef R_XLEN_T_MAX
typedef int R_xlen_t;
#define R_XLEN_T_MAX R_LEN_T_MAX
#ifndef xlength
#define xlength length
#endif
#endif


/* With strict headers (the default in R (>=4.5.0)),
* the prefixed variants R_Calloc and R_Free
* must be used instead of Calloc and Free. However, the prefixed variants
* do not exist prior to R 3.4.0, so we check whether strict headers are used and
* apply the legacy functions if not.
* If future versions of R remove the un-prefixed variants, this workaround will fail.
* In such a case, we must enforce the prefixed variants and increase the version
* requirement of the package to R 3.4.0.
*
*/
#if (R_VERSION >= R_Version(4, 5, 0))
#define R_CALLOC(num, size) R_Calloc(num, size)
#define R_FREE(ptr) R_Free(ptr)
#else
#define R_CALLOC(num, size) Calloc(num, size)
#define R_FREE(ptr) Free(ptr)
#endif


/* define NA_R_XLEN_T */
#ifdef LONG_VECTOR_SUPPORT
#define R_XLEN_T_MIN -R_XLEN_T_MAX-1
Expand Down
6 changes: 3 additions & 3 deletions src/diff2_lowlevel_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ void CONCAT_MACROS(diff2, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nx,
}
}

ans[ii] = X_DIFF(xvalue2, xvalue1);
ans[ii] = X_DIFF(xvalue2, xvalue1);
}
} else {
/* Allocate temporary work vector (to hold intermediate differences) */
tmp = R_CALLOC(nidxs - lag, X_C_TYPE);
tmp = R_Calloc(nidxs - lag, X_C_TYPE);

/* (a) First order of differences */
for (ii=0; ii < nidxs-lag; ii++) {
Expand Down Expand Up @@ -115,7 +115,7 @@ void CONCAT_MACROS(diff2, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nx,
}

/* Deallocate temporary work vector */
R_FREE(tmp);
R_Free(tmp);
} /* if (differences ...) */
}

Expand Down
4 changes: 2 additions & 2 deletions src/rowDiffs_lowlevel_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ void CONCAT_MACROS(rowDiffs, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nrow, R_xlen_t
nrow_tmp = nrows - lag;
ncol_tmp = ncols;
}
tmp = R_CALLOC(nrow_tmp*ncol_tmp, X_C_TYPE);
tmp = R_Calloc(nrow_tmp*ncol_tmp, X_C_TYPE);

/* (a) First order of differences */
DIFF_X_MATRIX_TYPE(x, nrow, rows, nrows, rowsHasNA, cols, ncols, colsHasNA, byrow, lag, tmp, nrow_tmp, ncol_tmp);
Expand All @@ -253,7 +253,7 @@ void CONCAT_MACROS(rowDiffs, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nrow, R_xlen_t
DIFF_X_MATRIX(tmp, nrow_tmp, ncol_tmp, byrow, lag, ans, nrow_ans, ncol_ans);

/* Deallocate temporary work matrix */
R_FREE(tmp);
R_Free(tmp);
} /* if (differences ...) */
}

Expand Down
6 changes: 3 additions & 3 deletions src/validateIndices_lowlevel_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ R_xlen_t* CONCAT_MACROS(validateIndices, X_C_SIGNATURE)(X_C_TYPE *idxs, R_xlen_t

// state < 0
// use filter as bitset to find out all required idxs
Rboolean *filter = R_CALLOC(maxIdx, Rboolean);
Rboolean *filter = R_Calloc(maxIdx, Rboolean);
count = maxIdx;
memset(filter, 0, maxIdx*sizeof(Rboolean)); // set to FALSE
for (ii = 0; ii < nidxs; ++ ii) {
Expand All @@ -110,7 +110,7 @@ R_xlen_t* CONCAT_MACROS(validateIndices, X_C_SIGNATURE)(X_C_TYPE *idxs, R_xlen_t

*ansNidxs = count;
if (count == 0) {
R_FREE(filter);
R_Free(filter);
return NULL;
}

Expand All @@ -123,7 +123,7 @@ R_xlen_t* CONCAT_MACROS(validateIndices, X_C_SIGNATURE)(X_C_TYPE *idxs, R_xlen_t

// fill required idxs into ans
// NOTE: braces is needed here, because of macro-defined function
RETURN_VALIDATED_ANS(R_xlen_t, upperBound, !filter[ii], ii, R_FREE(filter););
RETURN_VALIDATED_ANS(R_xlen_t, upperBound, !filter[ii], ii, R_Free(filter););
}


Expand Down
42 changes: 21 additions & 21 deletions src/weightedMedian_lowlevel_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ double CONCAT_MACROS(weightedMedian, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nx, do
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Weights */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
wtmp =R_CALLOC(nidxs, double);
wtmp =R_Calloc(nidxs, double);

/* Check for missing, negative, and infinite weights */
nxt = 0;
Expand All @@ -48,7 +48,7 @@ double CONCAT_MACROS(weightedMedian, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nx, do
weight = R_INDEX_GET(w, ((idxs == NULL) ? (ii) : idxs[ii]), NA_REAL, 1);
if (ISNAN(weight)) {
if (!narm) {
R_FREE(wtmp);
R_Free(wtmp);
return NA_REAL;
}
} else if (weight <= 0) {
Expand All @@ -66,7 +66,7 @@ double CONCAT_MACROS(weightedMedian, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nx, do
value = R_INDEX_GET(x, ((idxs == NULL) ? (jj) : idxs[jj]), X_NA, idxsHasNA);
if (X_ISNAN(value)) {
if (!narm) {
R_FREE(wtmp);
R_Free(wtmp);
return NA_REAL;
}
} else {
Expand All @@ -76,7 +76,7 @@ double CONCAT_MACROS(weightedMedian, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nx, do
}
} else if (ISNAN(weight)) {
if (!narm) {
R_FREE(wtmp);
R_Free(wtmp);
return NA_REAL;
}
}
Expand All @@ -88,7 +88,7 @@ double CONCAT_MACROS(weightedMedian, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nx, do
value = R_INDEX_GET(x, ((idxs == NULL) ? (ii) : idxs[ii]), X_NA, idxsHasNA);
if (X_ISNAN(value)) {
if (!narm) {
R_FREE(wtmp);
R_Free(wtmp);
return NA_REAL;
}
} else {
Expand All @@ -106,15 +106,15 @@ double CONCAT_MACROS(weightedMedian, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nx, do

/* Nothing to do? */
if (nxt == 0) {
R_FREE(wtmp);
R_Free(wtmp);
return NA_REAL;
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Copy (x,w) to work with and calculate total weight */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
xtmp =R_CALLOC(nxt, X_C_TYPE);
xtmp =R_Calloc(nxt, X_C_TYPE);
jj = 0;
wtotal = 0;
for (ii=0; ii < nidxs; ii++) {
Expand All @@ -138,8 +138,8 @@ double CONCAT_MACROS(weightedMedian, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nx, do
/* Early stopping? */
if (nx == 1) {
res = (double)x[0];
R_FREE(xtmp);
R_FREE(wtmp);
R_Free(xtmp);
R_Free(wtmp);
return res;
}

Expand All @@ -165,8 +165,8 @@ double CONCAT_MACROS(weightedMedian, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nx, do
res = ((double)x[half-1] + (double)x[half]) / 2;
}

R_FREE(xtmp);
R_FREE(wtmp);
R_Free(xtmp);
R_Free(wtmp);
return res;
}

Expand All @@ -176,12 +176,12 @@ double CONCAT_MACROS(weightedMedian, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nx, do
/* one) according to the reordered vector. */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* (a) Sort x */
idxs_int =R_CALLOC(nx, int);
idxs_int =R_Calloc(nx, int);
for (ii = 0; ii < nx; ii++) idxs_int[ii] = ii;
X_QSORT_I(x, idxs_int, 1, nx);

/* (b) Normalized cumulative weights */
wcum =R_CALLOC(nx, double);
wcum =R_Calloc(nx, double);
tmp_d2 = 0;
/* Index where cumulative weight passed 1/2 */
half = nx+1; /* Default is last */
Expand Down Expand Up @@ -209,16 +209,16 @@ double CONCAT_MACROS(weightedMedian, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nx, do
}
}
}
R_FREE(wtmp);
R_FREE(idxs_int);
R_Free(wtmp);
R_Free(idxs_int);


/* Two special cases where more than half of the total weight is at
a) the first, or b) the last value */
if (half == 0 || half == nx) {
res = (double)x[half];
R_FREE(wcum);
R_FREE(xtmp);
R_Free(wcum);
R_Free(xtmp);
return res;
}

Expand All @@ -242,8 +242,8 @@ double CONCAT_MACROS(weightedMedian, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nx, do
/* The corresponding x value */
res = dx + x[half];

R_FREE(wcum);
R_FREE(xtmp);
R_Free(wcum);
R_Free(xtmp);

return res;
}
Expand Down Expand Up @@ -290,8 +290,8 @@ printf("x[half+(-1:1)]=c(%g, %g, %g)\n", x[half-1-1], x[half-1], x[half-1+1]);
}
}

R_FREE(wcum);
R_FREE(xtmp);
R_Free(wcum);
R_Free(xtmp);

return res;
}
Expand Down