-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathcoerce.c
More file actions
165 lines (145 loc) · 3.76 KB
/
coerce.c
File metadata and controls
165 lines (145 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#define R_NO_REMAP
#include <R.h>
#include <Rinternals.h>
#include <stdio.h>
#include "conditions.h"
void cant_coerce(SEXP from, SEXP to, int i) {
const char* to_friendly;
switch(TYPEOF(to)) {
case INTSXP:
to_friendly = "an integer";
break;
case REALSXP:
to_friendly = "a double";
break;
case STRSXP:
to_friendly = "a string";
break;
case LGLSXP:
to_friendly = "a logical";
break;
case RAWSXP:
to_friendly = "a raw vector";
break;
default:
to_friendly = Rf_type2char(TYPEOF(to));
}
Rf_errorcall(
R_NilValue,
"Can't coerce from %s to %s.",
rlang_obj_type_friendly_full(from, false, false),
to_friendly
);
}
int real_to_logical(double x, SEXP from, SEXP to, int i) {
if (R_IsNA(x)) {
return NA_LOGICAL;
} else if (x == 0) {
return 0;
} else if (x == 1) {
return 1;
} else {
cant_coerce(from, to, i);
return 0;
}
}
int real_to_integer(double x, SEXP from, SEXP to, int i) {
if (R_IsNA(x)) {
return NA_INTEGER;
}
int out = x;
if (out == x) {
return out;
} else {
cant_coerce(from, to, i);
return 0;
}
}
int integer_to_logical(double x, SEXP from, SEXP to, int i) {
if (x == NA_INTEGER) {
return NA_LOGICAL;
} else if (x == 0) {
return 0;
} else if (x == 1) {
return 1;
} else {
cant_coerce(from, to, i);
return 0;
}
}
double logical_to_real(int x) {
return (x == NA_LOGICAL) ? NA_REAL : x;
}
double integer_to_real(int x) {
return (x == NA_INTEGER) ? NA_REAL : x;
}
void deprecate_to_char(const char* type_char) {
SEXP type = PROTECT(Rf_mkString(type_char));
SEXP fun = PROTECT(Rf_lang3(Rf_install(":::"), Rf_install("purrr"), Rf_install("deprecate_to_char")));
SEXP call = PROTECT(Rf_lang2(fun, type));
Rf_eval(call, R_GlobalEnv);
UNPROTECT(3);
}
SEXP logical_to_char(int x, SEXP from, SEXP to, int i) {
if (x == NA_LOGICAL) {
return NA_STRING;
} else {
cant_coerce(from, to, i);
return 0;
}
}
void set_vector_value(SEXP to, int i, SEXP from, int j) {
switch(TYPEOF(to)) {
case LGLSXP:
switch(TYPEOF(from)) {
case LGLSXP: LOGICAL(to)[i] = LOGICAL(from)[j]; break;
case INTSXP: LOGICAL(to)[i] = integer_to_logical(INTEGER(from)[j], from, to, i); break;
case REALSXP: LOGICAL(to)[i] = real_to_logical(REAL(from)[j], from, to, i); break;
default: cant_coerce(from, to, i);
}
break;
case INTSXP:
switch(TYPEOF(from)) {
case LGLSXP: INTEGER(to)[i] = LOGICAL(from)[j]; break;
case INTSXP: INTEGER(to)[i] = INTEGER(from)[j]; break;
case REALSXP: INTEGER(to)[i] = real_to_integer(REAL(from)[j], from, to, i); break;
default: cant_coerce(from, to, i);
}
break;
case REALSXP:
switch(TYPEOF(from)) {
case LGLSXP: REAL(to)[i] = logical_to_real(LOGICAL(from)[j]); break;
case INTSXP: REAL(to)[i] = integer_to_real(INTEGER(from)[j]); break;
case REALSXP: REAL(to)[i] = REAL(from)[j]; break;
default: cant_coerce(from, to, i);
}
break;
case STRSXP:
switch(TYPEOF(from)) {
case LGLSXP: SET_STRING_ELT(to, i, logical_to_char(LOGICAL(from)[j], from, to, i)); break;
case STRSXP: SET_STRING_ELT(to, i, STRING_ELT(from, j)); break;
default: cant_coerce(from, to, i);
}
break;
case VECSXP:
SET_VECTOR_ELT(to, i, from);
break;
case RAWSXP:
switch(TYPEOF(from)) {
case RAWSXP: RAW(to)[i] = RAW(from)[j]; break;
default: cant_coerce(from, to, i);
}
break ;
default: cant_coerce(from, to, i);
}
}
SEXP coerce_impl(SEXP x, SEXP type_) {
int n = Rf_length(x);
SEXPTYPE type = Rf_str2type(CHAR(Rf_asChar(type_)));
SEXP out = PROTECT(Rf_allocVector(type, n));
for (int i = 0; i < n; ++i) {
set_vector_value(out, i, x, i);
}
UNPROTECT(1);
return out;
}