Skip to content

Commit

Permalink
No longer use the non-API call SET_TYPEOF(); allocLang() (with a work…
Browse files Browse the repository at this point in the history
…around for older R) is used instead.

fixes #575
  • Loading branch information
krivit committed Sep 10, 2024
1 parent 7dbebbe commit c5180b3
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/etamap.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Copyright 2003-2023 Statnet Commons
*/
#include "ergm_etamap.h"
#include <Rversion.h>

#define SETUP_CALL(fun) \
SEXP cm = VECTOR_ELT(curved, i); \
Expand All @@ -28,6 +29,23 @@
SETCAR(pos, cov);


/*
A local implementation of allocLang(), recommended by Writing R
Extensions.
TODO: Delete in mid 2026.
*/
#if R_VERSION < R_Version(4, 4, 1)
static inline SEXP allocLang(int n)
{
if (n > 0)
return LCONS(R_NilValue, allocList(n - 1));
else
return R_NilValue;
}
#endif
/* End local implementation of allocLang(). */

SEXP ergm_eta_wrapper(SEXP thetaR, SEXP etamap){
unsigned int neta = asInteger(getListElement(etamap, "etalength"));
SEXP etaR = PROTECT(allocVector(REALSXP, neta));
Expand Down Expand Up @@ -56,9 +74,8 @@ void ergm_eta(double *theta, SEXP etamap, double *eta){
unsigned int ncurved = length(curved);

if(ncurved){
SEXP call = PROTECT(allocList(4));
SET_TYPEOF(call, LANGSXP);

SEXP call = PROTECT(allocLang(4));

for(unsigned int i = 0; i < ncurved; i++){
SETUP_CALL(map);
memcpy(eta1+to, REAL(eval(call, R_EmptyEnv)), nto*sizeof(double));
Expand Down Expand Up @@ -100,9 +117,8 @@ void ergm_etagrad(double *theta, SEXP etamap, double *etagrad){
unsigned int ncurved = length(curved);

if(ncurved){
SEXP call = PROTECT(allocList(4));
SET_TYPEOF(call, LANGSXP);

SEXP call = PROTECT(allocLang(4));

for(unsigned int i = 0; i < ncurved; i++){
SETUP_CALL(gradient);
double *g = REAL(eval(call, R_EmptyEnv));
Expand Down Expand Up @@ -158,9 +174,8 @@ void ergm_etagradmult(double *theta, double *v, unsigned int nv, SEXP etamap, do
unsigned int ncurved = length(curved);

if(ncurved){
SEXP call = PROTECT(allocList(4));
SET_TYPEOF(call, LANGSXP);

SEXP call = PROTECT(allocLang(4));

for(unsigned int i = 0; i < ncurved; i++){
SETUP_CALL(gradient);
double *g = REAL(eval(call, R_EmptyEnv));
Expand Down

0 comments on commit c5180b3

Please sign in to comment.