-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathnaming.c
265 lines (238 loc) · 10.7 KB
/
naming.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "naming.h"
void setNames(SEXP vec/*Answer vector*/, SEXP namesVec, R_xlen_t length, R_xlen_t *subscripts) {
if (length == 0) {
/* Zero-length names attribute? Keep behavior same as base R function */
return;
}
if (subscripts == NULL) {
namesgets(vec, namesVec);
} else {
SEXP ansNames = PROTECT(allocVector(STRSXP, length));
R_xlen_t i, thisIdx;
for (i = 0; i < length; i++) {
thisIdx = subscripts[i];
if (thisIdx == NA_R_XLEN_T) {
SET_STRING_ELT(ansNames, i, NA_STRING);
}
else {
SEXP eltElement = STRING_ELT(namesVec, thisIdx);
SET_STRING_ELT(ansNames, i, eltElement);
}
}
namesgets(vec, ansNames);
UNPROTECT(1);
}
}
void setNamesDiff(SEXP vec/* Answer vector*/, SEXP namesVec, R_xlen_t length, R_xlen_t length_ans, R_xlen_t *subscripts) {
/* For some reason, base::diff() actually sets an empty name attribute
when the argument is a name character of length zero, so
we skip the special case handled in setNames()
*/
SEXP ansNames = PROTECT(allocVector(STRSXP, length_ans));
R_xlen_t j = 0;
if (subscripts == NULL) {
for (R_xlen_t i = (length - length_ans); i < length; i++) {
SEXP eltElement = STRING_ELT(namesVec, i);
SET_STRING_ELT(ansNames, j++, eltElement);
}
} else {
R_xlen_t thisIdx;
for (R_xlen_t i = (length - length_ans); i < length; i++) {
thisIdx = subscripts[i];
if (thisIdx == NA_R_XLEN_T) {
SET_STRING_ELT(ansNames, j++, NA_STRING);
}
else {
SEXP eltElement = STRING_ELT(namesVec, thisIdx);
SET_STRING_ELT(ansNames, j++, eltElement);
}
}
}
namesgets(vec, ansNames);
UNPROTECT(1);
}
void setDimnames(SEXP mat/*Answer matrix*/, SEXP dimnames, R_xlen_t nrows,
R_xlen_t *crows, R_xlen_t ncols, R_xlen_t *ccols, Rboolean reverseDimnames) {
SEXP rownames = VECTOR_ELT(dimnames, reverseDimnames ? 1 : 0);
SEXP colnames = VECTOR_ELT(dimnames, reverseDimnames ? 0 : 1);
/* In case both elements of the dimnames is NULL, we disregard the name
attribute completely in order to conform to base R behavior */
if (rownames == R_NilValue && colnames == R_NilValue) {
return;
}
if (crows == NULL && ccols == NULL && nrows > 0 && ncols > 0) {
dimnamesgets(mat, dimnames);
return;
}
SEXP ansDimnames = PROTECT(allocVector(VECSXP, 2));
if (nrows == 0 || rownames == R_NilValue) {
SET_VECTOR_ELT(ansDimnames, 0, R_NilValue);
} else if (crows == NULL) {
SET_VECTOR_ELT(ansDimnames, 0, rownames);
} else {
SEXP ansRownames = PROTECT(allocVector(STRSXP, nrows));
R_xlen_t i, thisIdx;
for (i = 0; i < nrows; i++) {
thisIdx = crows[i];
if (thisIdx == NA_R_XLEN_T) {
SET_STRING_ELT(ansRownames, i, NA_STRING);
}
else {
SEXP eltElement = STRING_ELT(rownames, thisIdx);
SET_STRING_ELT(ansRownames, i, eltElement);
}
}
SET_VECTOR_ELT(ansDimnames, 0, ansRownames);
UNPROTECT(1);
}
if (ncols == 0 || colnames == R_NilValue) {
SET_VECTOR_ELT(ansDimnames, 1, R_NilValue);
} else if (ccols == NULL) {
SET_VECTOR_ELT(ansDimnames, 1, colnames);
} else {
if (colnames != R_NilValue) {
SEXP ansColnames = PROTECT(allocVector(STRSXP, ncols));
R_xlen_t i, thisIdx;
for (i = 0; i < ncols; i++) {
thisIdx = ccols[i];
if (thisIdx == NA_R_XLEN_T) {
SET_STRING_ELT(ansColnames, i, NA_STRING);
}
else {
SEXP eltElement = STRING_ELT(colnames, thisIdx);
SET_STRING_ELT(ansColnames, i, eltElement);
}
}
SET_VECTOR_ELT(ansDimnames, 1, ansColnames);
UNPROTECT(1);
}
}
dimnamesgets(mat, ansDimnames);
UNPROTECT(1);
}
void set_rowDiffs_Dimnames(SEXP mat/*Answer matrix*/, SEXP dimnames, R_xlen_t nrows,
R_xlen_t *crows, R_xlen_t ncols, R_xlen_t ncol_ans, R_xlen_t *ccols) {
if (nrows == 0 && ncol_ans == 0) {
/* Zero-length attributes? Keep behavior same as base R function */
return;
}
SEXP rownames = VECTOR_ELT(dimnames, 0);
SEXP colnames = VECTOR_ELT(dimnames, 1);
/* In case both elements of the dimnames is NULL, we disregard the name
attribute completely in order to conform to base R behavior */
if (rownames == R_NilValue && colnames == R_NilValue) {
return;
}
SEXP ansDimnames = PROTECT(allocVector(VECSXP, 2));
if (nrows == 0 || rownames == R_NilValue) {
SET_VECTOR_ELT(ansDimnames, 0, R_NilValue);
} else if (crows == NULL) {
SET_VECTOR_ELT(ansDimnames, 0, rownames);
} else {
SEXP ansRownames = PROTECT(allocVector(STRSXP, nrows));
R_xlen_t i, thisIdx;
for (i = 0; i < nrows; i++) {
thisIdx = crows[i];
if (thisIdx == NA_R_XLEN_T) {
SET_STRING_ELT(ansRownames, i, NA_STRING);
}
else {
SEXP eltElement = STRING_ELT(rownames, thisIdx);
SET_STRING_ELT(ansRownames, i, eltElement);
}
}
SET_VECTOR_ELT(ansDimnames, 0, ansRownames);
UNPROTECT(1);
}
if (ncol_ans == 0 || colnames == R_NilValue) {
SET_VECTOR_ELT(ansDimnames, 1, R_NilValue);
} else {
SEXP ansColnames = PROTECT(allocVector(STRSXP, ncol_ans));
R_xlen_t i, j = 0;
if (ccols == NULL) {
for (i = (ncols - ncol_ans); i < ncols; i++) {
SEXP eltElement = STRING_ELT(colnames, i);
SET_STRING_ELT(ansColnames, j++, eltElement);
}
} else {
R_xlen_t thisIdx;
for (i = (ncols - ncol_ans); i < ncols; i++) {
thisIdx = ccols[i];
if (thisIdx == NA_R_XLEN_T) {
SET_STRING_ELT(ansColnames, j++, NA_STRING);
}
else {
SEXP eltElement = STRING_ELT(colnames, thisIdx);
SET_STRING_ELT(ansColnames, j++, eltElement);
}
}
}
SET_VECTOR_ELT(ansDimnames, 1, ansColnames);
UNPROTECT(1);
}
dimnamesgets(mat, ansDimnames);
UNPROTECT(1);
}
void set_colDiffs_Dimnames(SEXP mat/*Answer matrix*/, SEXP dimnames, R_xlen_t nrows, R_xlen_t nrow_ans,
R_xlen_t *crows, R_xlen_t ncols, R_xlen_t *ccols) {
if (nrow_ans == 0 && ncols == 0) {
/* Zero-length attributes? Keep behavior same as base R function */
return;
}
SEXP rownames = VECTOR_ELT(dimnames, 0);
SEXP colnames = VECTOR_ELT(dimnames, 1);
/* In case both elements of the dimnames is NULL, we disregard the name
attribute completely in order to conform to base R behavior */
if (rownames == R_NilValue && colnames == R_NilValue) {
return;
}
SEXP ansDimnames = PROTECT(allocVector(VECSXP, 2));
if (nrow_ans == 0 || rownames == R_NilValue) {
SET_VECTOR_ELT(ansDimnames, 0, R_NilValue);
} else {
SEXP ansRownames = PROTECT(allocVector(STRSXP, nrow_ans));
R_xlen_t i, j = 0;
if (crows == NULL) {
for (i = (nrows - nrow_ans); i < nrows; i++) {
SEXP eltElement = STRING_ELT(rownames, i);
SET_STRING_ELT(ansRownames, j++, eltElement);
}
} else {
R_xlen_t thisIdx;
for (i = (nrows - nrow_ans); i < nrows; i++) {
thisIdx = crows[i];
if (thisIdx == NA_R_XLEN_T) {
SET_STRING_ELT(ansRownames, j++, NA_STRING);
}
else {
SEXP eltElement = STRING_ELT(rownames, thisIdx);
SET_STRING_ELT(ansRownames, j++, eltElement);
}
}
}
SET_VECTOR_ELT(ansDimnames, 0, ansRownames);
UNPROTECT(1);
}
if (ncols == 0 || colnames == R_NilValue) {
SET_VECTOR_ELT(ansDimnames, 1, R_NilValue);
} else if (ccols == NULL) {
SET_VECTOR_ELT(ansDimnames, 1, colnames);
} else {
SEXP ansColnames = PROTECT(allocVector(STRSXP, ncols));
R_xlen_t i, thisIdx;
for (i = 0; i < ncols; i++) {
thisIdx = ccols[i];
if (thisIdx == NA_R_XLEN_T) {
SET_STRING_ELT(ansColnames, i, NA_STRING);
}
else {
SEXP eltElement = STRING_ELT(colnames, thisIdx);
SET_STRING_ELT(ansColnames, i, eltElement);
}
}
SET_VECTOR_ELT(ansDimnames, 1, ansColnames);
UNPROTECT(1);
}
dimnamesgets(mat, ansDimnames);
UNPROTECT(1);
}