1 /*
2 # C-Code for chunk utilities
3 # (c) 2012 Jens Oehlsch�gel
4 # Licence: GPL2
5 # Provided 'as is', use at your own risk
6 */
7 
8 #include <R.h>
9 #define USE_RINTERNALS
10 #include <Rinternals.h>
11 
R_bit_vecseq(SEXP x_,SEXP y_)12 SEXP R_bit_vecseq(SEXP x_, SEXP y_)
13 {
14     int *x,*y,*ret;
15     register int val, lim;
16     R_len_t K,k,n,i;
17     SEXP ret_;
18     // if (!isInteger(x_))
19       // error("x must be an integer vector");
20     // if (!isInteger(y_))
21       // error("y must be an integer vector");
22     K = LENGTH(x_);
23     // if (LENGTH(y_) != K) error("x and y must be the same length");
24 
25     x = INTEGER(x_);
26     y = INTEGER(y_);
27 
28     n = 0;
29     for (k=0; k<K; k++)
30       n += x[k] < y[k] ? y[k] - x[k] + 1 : x[k] - y[k] + 1;
31     ret_ = PROTECT(allocVector(INTSXP, n));
32     ret = INTEGER(ret_);
33 
34     i = 0;
35     for (k=0; k<K; k++) {
36       lim = y[k];
37       val = x[k];
38       if (val < lim){
39         while(val<=lim){
40           ret[i++] = val++;
41         }
42       }else{
43         while(val>=lim){
44           ret[i++] = val--;
45         }
46       }
47     }
48     UNPROTECT(1);
49     return(ret_);
50 }
51