Skip to content

Commit 7d24765

Browse files
authored
C API compliance (#1618)
* Remove usage of `DATAPTR()` * NEWS bullet
1 parent 25d0ff7 commit 7d24765

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# tidyr (development version)
22

3+
* tidyr is now compliant with R's C API (#1618).
4+
35
* Fixed an internal error in `pivot_wider()` (#1609, @krlmlr).
46

57
* The base pipe is now used throughout the documentation (#1613).

src/melt.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,22 @@ cpp11::strings make_variable_column_character(cpp11::strings x, int nrow) {
134134
}
135135

136136
// Concatenate vectors for the 'value' column
137-
#define DO_CONCATENATE(CTYPE) \
138-
{ \
139-
memcpy((char*)DATAPTR(output) + i* nrow * sizeof(CTYPE), \
140-
(char*)DATAPTR(tmp), \
141-
nrow * sizeof(CTYPE)); \
142-
break; \
137+
#define DO_CONCATENATE(CTYPE, CONST_DEREF, DEREF) \
138+
{ \
139+
memcpy( \
140+
(char*)DEREF(output) + (i * nrow * sizeof(CTYPE)), \
141+
(const char*)CONST_DEREF(tmp), \
142+
nrow * sizeof(CTYPE) \
143+
); \
144+
break; \
145+
}
146+
147+
#define DO_CONCATENATE_BARRIER(GET, SET) \
148+
{ \
149+
for (int j = 0; j < nrow; ++j) { \
150+
SET(output, i * nrow + j, GET(tmp, j)); \
151+
} \
152+
break; \
143153
}
144154

145155
SEXP concatenate(const cpp11::data_frame& x, cpp11::integers ind, bool factorsAsStrings) {
@@ -185,27 +195,19 @@ SEXP concatenate(const cpp11::data_frame& x, cpp11::integers ind, bool factorsAs
185195

186196
switch (max_type) {
187197
case INTSXP:
188-
DO_CONCATENATE(int);
198+
DO_CONCATENATE(int, INTEGER_RO, INTEGER);
189199
case REALSXP:
190-
DO_CONCATENATE(double);
200+
DO_CONCATENATE(double, REAL_RO, REAL);
191201
case LGLSXP:
192-
DO_CONCATENATE(int);
202+
DO_CONCATENATE(int, LOGICAL_RO, LOGICAL);
193203
case CPLXSXP:
194-
DO_CONCATENATE(Rcomplex);
195-
case STRSXP: {
196-
for (int j = 0; j < nrow; ++j) {
197-
SET_STRING_ELT(output, i * nrow + j, STRING_ELT(tmp, j));
198-
}
199-
break;
200-
}
201-
case VECSXP: {
202-
for (int j = 0; j < nrow; ++j) {
203-
SET_VECTOR_ELT(output, i * nrow + j, VECTOR_ELT(tmp, j));
204-
}
205-
break;
206-
}
204+
DO_CONCATENATE(Rcomplex, COMPLEX_RO, COMPLEX);
205+
case STRSXP:
206+
DO_CONCATENATE_BARRIER(STRING_ELT, SET_STRING_ELT);
207+
case VECSXP:
208+
DO_CONCATENATE_BARRIER(VECTOR_ELT, SET_VECTOR_ELT);
207209
default:
208-
cpp11::stop("All columns be atomic vectors or lists (not %s)", Rf_type2char(max_type));
210+
cpp11::stop("All columns be atomic vectors or lists (not %s)", Rf_type2char(max_type));
209211
}
210212
}
211213

0 commit comments

Comments
 (0)