forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.cpp
455 lines (377 loc) · 12.9 KB
/
set.cpp
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include "pch.h"
#include <dplyr/main.h>
#include <boost/scoped_ptr.hpp>
#include <tools/match.h>
#include <tools/collapse.h>
#include <tools/BoolResult.h>
#include <dplyr/visitor_set/VisitorSetIndexSet.h>
#include <dplyr/visitor_set/VisitorSetIndexMap.h>
#include <dplyr/visitors/join/Column.h>
#include <dplyr/visitors/join/JoinVisitor.h>
#include <dplyr/visitors/join/JoinVisitorImpl.h>
#include <dplyr/visitors/join/DataFrameJoinVisitors.h>
#include <tools/train.h>
#include <dplyr/data/GroupedDataFrame.h>
class RowTrack {
public:
RowTrack(const std::string& msg, int max_count_ = 10) : ss(), count(0), max_count(max_count_) {
ss << msg;
}
void record(int i) {
if (count > max_count) return;
if (count) ss << ", ";
int idx = i >= 0 ? (i + 1) : -i;
ss << idx;
if (count == max_count) ss << "[...]";
count++;
}
bool empty() const {
return count == 0;
}
std::string str() const {
return ss.str();
}
private:
std::stringstream ss;
int count;
int max_count;
};
// [[Rcpp::export(rng = false)]]
dplyr::BoolResult compatible_data_frame_nonames(Rcpp::DataFrame x, Rcpp::DataFrame y, bool convert) {
int n = x.size();
if (n != y.size())
return dplyr::no_because(tfm::format("different number of columns : %d x %d", n, y.size()));
if (convert) {
for (int i = 0; i < n; i++) {
try {
boost::scoped_ptr<dplyr::JoinVisitor> v(
dplyr::join_visitor(
Column(x[i], dplyr::SymbolString("x")), Column(y[i], dplyr::SymbolString("y")), true, true
)
);
} catch (...) {
return dplyr::no_because("incompatible");
}
}
} else {
for (int i = 0; i < n; i++) {
SEXP xi = x[i], yi = y[i];
if (TYPEOF(xi) != TYPEOF(yi))
return dplyr::no_because("incompatible types");
if (TYPEOF(xi) == INTSXP) {
if (Rf_inherits(xi, "factor") && Rf_inherits(yi, "factor")) {
if (dplyr::same_levels(xi, yi)) continue;
return dplyr::no_because("factors with different levels");
}
if (Rf_inherits(xi, "factor")) return dplyr::no_because("cannot compare factor and integer");
if (Rf_inherits(yi, "factor")) return dplyr::no_because("cannot compare factor and integer");
}
}
}
return dplyr::yes();
}
bool same_factor_levels(SEXP x, SEXP y, std::stringstream& ss, const dplyr::SymbolString& name) {
bool res = dplyr::same_levels(x, y);
if (!res) {
ss << "Factor levels not equal for column `" << name.get_utf8_cstring() << "`";
}
return res;
}
bool type_compatible(SEXP x, SEXP y) {
// if one is a matrix but not the other, the types are not compatible
if (Rf_isMatrix(x) + Rf_isMatrix(y) == 1) {
return false;
}
if (Rf_inherits(x, "Date")) return Rf_inherits(y, "Date");
switch (TYPEOF(x)) {
case RAWSXP:
return TYPEOF(y) == RAWSXP;
case LGLSXP:
return TYPEOF(y) == LGLSXP;
case CPLXSXP:
return TYPEOF(y) == CPLXSXP;
case INTSXP:
if (Rf_isFactor(x)) {
return TYPEOF(y) == STRSXP || Rf_isFactor(y);
} else if (Rf_inherits(x, "Date")) {
return Rf_inherits(y, "Date");
} else {
return !Rf_isFactor(y) && (TYPEOF(y) == INTSXP || TYPEOF(y) == REALSXP);
}
case REALSXP:
return TYPEOF(y) == INTSXP || TYPEOF(y) == REALSXP;
case STRSXP:
return TYPEOF(y) == STRSXP || Rf_isFactor(y);
case VECSXP:
if (Rf_inherits(x, "data.frame")) {
// TODO: also recurse into the df to check if
// - same names
// - same type for each column
return Rf_inherits(y, "data.frame");
} else {
return !Rf_inherits(y, "data.frame");
}
default:
break;
}
return false;
}
bool type_same(SEXP x, SEXP y, std::stringstream& ss, const dplyr::SymbolString& name) {
// if one is a matrix but not the other, the types are not compatible
if (Rf_isMatrix(x) + Rf_isMatrix(y) == 1) {
return false;
}
if (Rf_inherits(x, "Date")) return Rf_inherits(y, "Date");
switch (TYPEOF(x)) {
case RAWSXP:
return TYPEOF(y) == RAWSXP;
case LGLSXP:
return TYPEOF(y) == LGLSXP;
case CPLXSXP:
return TYPEOF(y) == CPLXSXP;
case INTSXP:
if (Rf_isFactor(x)) {
return Rf_isFactor(y) && same_factor_levels(x, y, ss, name);
} else {
return !Rf_isFactor(y) && TYPEOF(y) == INTSXP;
}
case REALSXP:
if (Rf_inherits(x, "Date")) {
return Rf_inherits(y, "Date");
} else {
return TYPEOF(y) == REALSXP;
}
case STRSXP:
return TYPEOF(y) == STRSXP;
case VECSXP:
if (Rf_inherits(x, "data.frame")) {
// TODO: also recurse into the df to check if
// - same names
// - same type for each column
return Rf_inherits(y, "data.frame");
} else {
return !Rf_inherits(y, "data.frame");
}
default:
break;
}
return false;
}
std::string type_describe(SEXP x) {
if (Rf_isMatrix(x)) {
return "matrix";
} else if (Rf_inherits(x, "data.frame")) {
return dplyr::get_single_class(x);
} else if (Rf_inherits(x, "Date")) {
return "Date";
} else if (Rf_isFactor(x)) {
return dplyr::get_single_class(x);
} else {
return dplyr::get_single_class(x);
}
}
// [[Rcpp::export(rng = false)]]
dplyr::BoolResult compatible_data_frame(Rcpp::DataFrame x, Rcpp::DataFrame y, bool ignore_col_order = true, bool convert = false) {
int n = x.size();
Rcpp::Shield<SEXP> x_names(Rf_getAttrib(x, dplyr::symbols::names));
Rcpp::Shield<SEXP> y_names(Rf_getAttrib(y, dplyr::symbols::names));
bool null_x = Rf_isNull(x_names);
bool null_y = Rf_isNull(y_names);
if (null_x && !null_y) {
return dplyr::no_because("x does not have names, but y does");
} else if (null_y && !null_x) {
return dplyr::no_because("y does not have names, but x does");
} else if (null_x && null_y) {
return compatible_data_frame_nonames(x, y, convert);
}
Rcpp::CharacterVector names_x(x_names);
Rcpp::CharacterVector names_y(y_names);
Rcpp::CharacterVector names_y_not_in_x = setdiff(names_y, names_x);
Rcpp::CharacterVector names_x_not_in_y = setdiff(names_x, names_y);
if (!ignore_col_order) {
if (names_y_not_in_x.size() == 0 && names_x_not_in_y.size() == 0) {
// so the names are the same, check if they are in the same order
for (int i = 0; i < n; i++) {
if (names_x[i] != names_y[i]) {
return dplyr::no_because("Same column names, but different order");
}
}
}
}
Rcpp::CharacterVector why;
if (names_y_not_in_x.size()) {
std::stringstream ss;
ss << "Cols in y but not x: " << dplyr::collapse_utf8(names_y_not_in_x, ", ", "`") << ". ";
why.push_back(Rcpp::String(ss.str(), CE_UTF8));
}
if (names_x_not_in_y.size()) {
std::stringstream ss;
ss << "Cols in x but not y: " << dplyr::collapse_utf8(names_x_not_in_y, ", ", "`") << ". ";
why.push_back(Rcpp::String(ss.str(), CE_UTF8));
}
if (why.length() > 0) return dplyr::no_because(why);
Rcpp::Shield<SEXP> orders(dplyr::r_match(names_x, names_y));
int* p_orders = INTEGER(orders);
for (int i = 0; i < n; i++) {
dplyr::SymbolString name = names_x[i];
SEXP xi = x[i], yi = y[p_orders[i] - 1];
std::stringstream ss;
bool compatible = convert ? type_compatible(xi, yi) : type_same(xi, yi, ss, name);
if (!compatible) {
if (ss.str() == "") {
ss << "Incompatible type for column `"
<< name.get_utf8_cstring()
<< "`: x " << type_describe(xi)
<< ", y " << type_describe(yi);
}
why.push_back(Rcpp::String(ss.str(), CE_UTF8));
}
}
if (why.length() > 0) return dplyr::no_because(why);
return dplyr::yes();
}
// [[Rcpp::export(rng = false)]]
dplyr::BoolResult equal_data_frame(Rcpp::DataFrame x, Rcpp::DataFrame y, bool ignore_col_order = true, bool ignore_row_order = true, bool convert = false) {
dplyr::BoolResult compat = compatible_data_frame(x, y, ignore_col_order, convert);
if (!compat) return compat;
typedef dplyr::VisitorSetIndexMap<dplyr::DataFrameJoinVisitors, std::vector<int> > Map;
dplyr::SymbolVector x_names(Rf_getAttrib(x, dplyr::symbols::names));
dplyr::DataFrameJoinVisitors visitors(x, y, x_names, x_names, true, true);
Map map(visitors);
// train the map in both x and y
int nrows_x = x.nrows();
int nrows_y = y.nrows();
if (nrows_x != nrows_y)
return dplyr::no_because("Different number of rows");
if (x.size() == 0)
return dplyr::yes();
for (int i = 0; i < nrows_x; i++) map[i].push_back(i);
for (int i = 0; i < nrows_y; i++) map[-i - 1].push_back(-i - 1);
RowTrack track_x("Rows in x but not y: ");
RowTrack track_y("Rows in y but not x: ");
RowTrack track_mismatch("Rows with difference occurences in x and y: ");
bool ok = true;
Map::const_iterator it = map.begin();
for (; it != map.end(); ++it) {
// retrieve the indices ( -ves for y, +ves for x )
const std::vector<int>& chunk = it->second;
int n = chunk.size();
int count_left = 0, count_right = 0;
for (int i = 0; i < n; i++) {
if (chunk[i] < 0)
count_right++;
else
count_left++;
}
if (count_right == 0) {
track_x.record(chunk[0]);
ok = false;
} else if (count_left == 0) {
track_y.record(chunk[0]);
ok = false;
} else if (count_left != count_right) {
track_mismatch.record(chunk[0]);
ok = false;
}
}
if (!ok) {
std::stringstream ss;
if (! track_x.empty()) ss << track_x.str() << ". ";
if (! track_y.empty()) ss << track_y.str() << ". ";
if (! track_mismatch.empty()) ss << track_mismatch.str();
return dplyr::no_because(Rcpp::CharacterVector::create(Rcpp::String(ss.str(), CE_UTF8)));
}
if (ok && ignore_row_order) return dplyr::yes();
if (!ignore_row_order) {
for (int i = 0; i < nrows_x; i++) {
if (!visitors.equal(i, -i - 1)) {
return dplyr::no_because("Same row values, but different order");
}
}
}
return dplyr::yes();
}
Rcpp::DataFrame reconstruct_metadata(Rcpp::DataFrame out, const Rcpp::DataFrame& x) {
if (Rcpp::is<dplyr::GroupedDataFrame>(x)) {
// go through the GroupedDataFrame class so that the groups attribute is generated
return dplyr::GroupedDataFrame(out, x).data();
} else {
// nothing to do for rowwise and natural data frames
return out;
}
}
// [[Rcpp::export(rng = false)]]
Rcpp::DataFrame union_data_frame(Rcpp::DataFrame x, Rcpp::DataFrame y) {
dplyr::BoolResult compat = compatible_data_frame(x, y, true, true);
if (!compat) {
Rcpp::stop("not compatible: %s", compat.why_not());
}
typedef dplyr::VisitorSetIndexSet<dplyr::DataFrameJoinVisitors> Set;
dplyr::SymbolVector x_names(Rf_getAttrib(x, dplyr::symbols::names));
dplyr::DataFrameJoinVisitors visitors(x, y, x_names, x_names, true, true);
Set set(visitors);
int n_x = x.nrows();
int n_y = y.nrows();
std::vector<int> indices;
indices.reserve(n_x + n_y);
for (int i = 0; i < n_x; i++) {
std::pair<Set::iterator, bool> inserted = set.insert(i);
if (inserted.second) {
indices.push_back(i);
}
}
for (int i = 0; i < n_y; i++) {
std::pair<Set::iterator, bool> inserted = set.insert(-i - 1);
if (inserted.second) {
indices.push_back(-i - 1);
}
}
return reconstruct_metadata(visitors.subset(indices, dplyr::get_class(x)), x);
}
// [[Rcpp::export(rng = false)]]
Rcpp::DataFrame intersect_data_frame(Rcpp::DataFrame x, Rcpp::DataFrame y) {
dplyr::BoolResult compat = compatible_data_frame(x, y, true, true);
if (!compat) {
Rcpp::stop("not compatible: %s", compat.why_not());
}
typedef dplyr::VisitorSetIndexSet<dplyr::DataFrameJoinVisitors> Set;
dplyr::SymbolVector x_names(Rf_getAttrib(x, dplyr::symbols::names));
dplyr::DataFrameJoinVisitors visitors(x, y, x_names, x_names, true, true);
Set set(visitors);
int n_x = x.nrows();
int n_y = y.nrows();
dplyr::train_insert_right(set, n_y);
std::vector<int> indices;
indices.reserve(std::min(n_x, n_y));
for (int i = 0; i < n_x; i++) {
Set::iterator it = set.find(i);
if (it != set.end()) {
indices.push_back(*it);
set.erase(it);
}
}
return reconstruct_metadata(visitors.subset(indices, dplyr::get_class(x)), x);
}
// [[Rcpp::export(rng = false)]]
Rcpp::DataFrame setdiff_data_frame(Rcpp::DataFrame x, Rcpp::DataFrame y) {
dplyr::BoolResult compat = compatible_data_frame(x, y, true, true);
if (!compat) {
Rcpp::stop("not compatible: %s", compat.why_not());
}
typedef dplyr::VisitorSetIndexSet<dplyr::DataFrameJoinVisitors> Set;
dplyr::SymbolVector y_names(Rf_getAttrib(y, dplyr::symbols::names));
dplyr::DataFrameJoinVisitors visitors(x, y, y_names, y_names, true, true);
Set set(visitors);
int n_x = x.nrows();
int n_y = y.nrows();
train_insert_right(set, n_y);
std::vector<int> indices;
indices.reserve(n_x);
for (int i = 0; i < n_x; i++) {
std::pair<Set::iterator, bool> inserted = set.insert(i);
if (inserted.second) {
indices.push_back(i);
}
}
return reconstruct_metadata(visitors.subset(indices, dplyr::get_class(x)), x);
}