From @zwj|08 @end|ng |rom gm@||@com  Wed Oct 23 06:45:29 2019
From: @zwj|08 @end|ng |rom gm@||@com (Wang Jiefei)
Date: Wed, 23 Oct 2019 00:45:29 -0400
Subject: [Rd] Unexpected behavior when using macro to loop over vector
Message-ID: <CAGiFhPNAvjx4_jNPc9VJVy3vJ3NhCMCPnvEt5whh5P6Ca-nuDA@mail.gmail.com>

Hi all,

I found an unexpected behavior when I was trying to use the macro defined
in "R_ext/Itermacros.h"  to loop over an atomic vector. Here is a minimum
example:

C++ code
```
#include "R_ext/Itermacros.h"
#define GET_REGION_BUFSIZE 2
//Redefine the macro since C++ is not happy with the implicit type
conversion
#define ITERATE_BY_REGION_PARTIAL(sx, px, idx, nb, etype, vtype, \
 strt, nfull, expr) do { \
const etype *px = (etype*)DATAPTR_OR_NULL(sx); \
if (px != NULL) { \
   R_xlen_t __ibr_n__ = strt + nfull; \
   R_xlen_t nb = __ibr_n__; \
   for (R_xlen_t idx = strt; idx < __ibr_n__; idx += nb) { \
expr \
    } \
} \
else ITERATE_BY_REGION_PARTIAL0(sx, px, idx, nb, etype, vtype, \
strt, nfull, expr); \
    } while (0)
// [[Rcpp::export]]
void C_testPrint(SEXP x) {
ITERATE_BY_REGION_PARTIAL(x, ptr, idx, nbatch, double, REAL, 1, 4, {
for (R_xlen_t i = 0; i < nbatch; i++)
Rprintf("idx: %lld, i: %lld, ptr:%f\n", idx, i, ptr[i]);
});
}
```

The function C_testPrint loops over its argument x and prints out one value
of x at each loop. The loop starts from the second element and ends in the
fifth element of x. I also redefine the buffer size to see the effect of
it. Here is my R code:

R code
```
> C_testPrint(as.numeric(1:10))
idx: 1, i: 0, ptr:2.000000
idx: 1, i: 1, ptr:3.000000
idx: 3, i: 0, ptr:4.000000
idx: 3, i: 1, ptr:5.000000
> C_testPrint(c(1,2,3,4,5,6,7,8,9,10))
idx: 1, i: 0, ptr:1.000000
idx: 1, i: 1, ptr:2.000000
idx: 1, i: 2, ptr:3.000000
idx: 1, i: 3, ptr:4.000000
idx: 1, i: 4, ptr:5.000000
```

There are two problems in the outputs:
1. The numbers of lines are different
2. The starting indices are not the same.

>From my understanding, the first output seems correct to me. The second is
not unexpected. I believe the differences are due to the accessibility of
the data pointer. Did I misunderstand and misuse the macro? Or is it a bug
in R? Here is my session info. My R is a bit outdated but the macro seems
unchanged in R 4.0. Thanks

```
> sessionInfo()
R Under development (unstable) (2019-08-22 r77060)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
```

	[[alternative HTML version deleted]]



[Rd] Unexpected behavior when using macro to loop over vector
Wang Jiefei