Matrix r5059
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1#include "Mdefines.h"
2
3#if R_VERSION < R_Version(4, 5, 0)
4int ANY_ATTRIB(SEXP x)
5{
6 return ATTRIB(x) != R_NilValue;
7}
8
9void CLEAR_ATTRIB(SEXP x)
10{
11 SET_ATTRIB(x, R_NilValue);
12 SET_OBJECT(x, 0);
13 UNSET_S4_OBJECT(x);
14 return;
15}
16#endif
17
18char *Matrix_sprintf(const char *format, ...)
19{
20 char *buf = R_alloc(Matrix_ErrorBufferSize, sizeof(char));
21 va_list args;
22 va_start(args, format);
23 vsnprintf(buf, Matrix_ErrorBufferSize, format, args);
24 va_end(args);
25 return buf;
26}
27
28int equalString(SEXP s1, SEXP s2, R_xlen_t n)
29{
30 SEXP s1_, s2_;
31 R_xlen_t j;
32 for (j = 0; j < n; ++j) {
33 s1_ = STRING_ELT(s1, j);
34 s2_ = STRING_ELT(s2, j);
35 if ((s1_ == NA_STRING) != (s2_ == NA_STRING) ||
36 strcmp(CHAR(s1_), CHAR(s2_)) != 0)
37 return 0;
38 }
39 return 1;
40}
41
42SEXP duplicateVector(SEXP x)
43{
44 SEXPTYPE type = TYPEOF(x);
45 R_xlen_t length = XLENGTH(x);
46 SEXP ans = Rf_allocVector(type, length);
47 switch (type) {
48 case RAWSXP:
49 memcpy( RAW(ans), RAW(x), sizeof( Rbyte) * (size_t) length);
50 break;
51 case LGLSXP:
52 memcpy(LOGICAL(ans), LOGICAL(x), sizeof( int) * (size_t) length);
53 break;
54 case INTSXP:
55 memcpy(INTEGER(ans), INTEGER(x), sizeof( int) * (size_t) length);
56 break;
57 case REALSXP:
58 memcpy( REAL(ans), REAL(x), sizeof( double) * (size_t) length);
59 break;
60 case CPLXSXP:
61 memcpy(COMPLEX(ans), COMPLEX(x), sizeof(Rcomplex) * (size_t) length);
62 break;
63 default:
64 break;
65 }
66 return ans;
67}
68
69SEXP allocZero(SEXPTYPE type, R_xlen_t length)
70{
71 SEXP ans = Rf_allocVector(type, length);
72 switch (type) {
73 case RAWSXP:
74 memset( RAW(ans), 0, sizeof( Rbyte) * (size_t) length);
75 break;
76 case LGLSXP:
77 memset(LOGICAL(ans), 0, sizeof( int) * (size_t) length);
78 break;
79 case INTSXP:
80 memset(INTEGER(ans), 0, sizeof( int) * (size_t) length);
81 break;
82 case REALSXP:
83 memset( REAL(ans), 0, sizeof( double) * (size_t) length);
84 break;
85 case CPLXSXP:
86 memset(COMPLEX(ans), 0, sizeof(Rcomplex) * (size_t) length);
87 break;
88 default:
89 break;
90 }
91 return ans;
92}
93
94SEXP allocUnit(SEXPTYPE type, R_xlen_t length)
95{
96 SEXP ans = Rf_allocVector(type, length);
97 R_xlen_t i;
98 switch (type) {
99 case RAWSXP:
100 {
101 Rbyte *pans = RAW(ans);
102 for (i = 0; i < length; ++i)
103 *(pans++) = 1;
104 break;
105 }
106 case LGLSXP:
107 {
108 int *pans = LOGICAL(ans);
109 for (i = 0; i < length; ++i)
110 *(pans++) = 1;
111 break;
112 }
113 case INTSXP:
114 {
115 int *pans = INTEGER(ans);
116 for (i = 0; i < length; ++i)
117 *(pans++) = 1;
118 break;
119 }
120 case REALSXP:
121 {
122 double *pans = REAL(ans);
123 for (i = 0; i < length; ++i)
124 *(pans++) = 1.0;
125 break;
126 }
127 case CPLXSXP:
128 {
129 Rcomplex *pans = COMPLEX(ans);
130 Rcomplex u; u.r = 1.0; u.i = 0.0;
131 for (i = 0; i < length; ++i)
132 *(pans++) = u;
133 break;
134 }
135 default:
136 break;
137 }
138 return ans;
139}
140
141SEXP allocSeqInt(int from, R_xlen_t length)
142{
143 SEXP ans = Rf_allocVector(INTSXP, length);
144 int *pans = INTEGER(ans);
145 R_xlen_t i;
146 for (i = 0; i < length; ++i)
147 *(pans++) = from++;
148 return ans;
149}
150
151void naToUnit(SEXP x)
152{
153 R_xlen_t i, length = XLENGTH(x);
154 switch (TYPEOF(x)) {
155 case LGLSXP:
156 {
157 int *px = LOGICAL(x);
158 for (i = 0; i < length; ++i) {
159 if (*px == NA_LOGICAL)
160 *px = 1;
161 px += 1;
162 }
163 break;
164 }
165 case INTSXP:
166 {
167 int *px = INTEGER(x);
168 for (i = 0; i < length; ++i) {
169 if (*px == NA_INTEGER)
170 *px = 1;
171 px += 1;
172 }
173 break;
174 }
175 case REALSXP:
176 {
177 double *px = REAL(x);
178 for (i = 0; i < length; ++i) {
179 if (ISNAN(*px))
180 *px = 1.0;
181 px += 1;
182 }
183 break;
184 }
185 case CPLXSXP:
186 {
187 Rcomplex *px = COMPLEX(x);
188 Rcomplex u; u.r = 1.0; u.i = 0.0;
189 for (i = 0; i < length; ++i) {
190 if (ISNAN((*px).r) || ISNAN((*px).i))
191 *px = u;
192 px += 1;
193 }
194 break;
195 }
196 default:
197 break;
198 }
199 return;
200}
#define Matrix_ErrorBufferSize
Definition Mdefines.h:35
#define TYPEOF(s)
Definition Mdefines.h:123
int equalString(SEXP s1, SEXP s2, R_xlen_t n)
Definition utils.c:28
SEXP allocZero(SEXPTYPE type, R_xlen_t length)
Definition utils.c:69
void naToUnit(SEXP x)
Definition utils.c:151
SEXP duplicateVector(SEXP x)
Definition utils.c:42
char * Matrix_sprintf(const char *format,...)
Definition utils.c:18
SEXP allocSeqInt(int from, R_xlen_t length)
Definition utils.c:141
SEXP allocUnit(SEXPTYPE type, R_xlen_t length)
Definition utils.c:94