forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind.cpp
246 lines (202 loc) · 6.84 KB
/
bind.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
#include <dplyr.h>
using namespace Rcpp ;
using namespace dplyr ;
class DataFrameAbleVector {
public:
DataFrameAbleVector() : data(){}
inline void push_back( SEXP x) {
data.push_back( DataFrameAble(x) ) ;
}
inline const DataFrameAble& operator[]( int i) const {
return data[i] ;
}
inline int size() const {
return data.size() ;
}
~DataFrameAbleVector(){
while (data.size()) data.pop_back();
}
private:
std::vector<DataFrameAble> data ;
} ;
template <typename Dots>
List rbind__impl( Dots dots, SEXP id = R_NilValue ){
int ndata = dots.size() ;
int n = 0 ;
DataFrameAbleVector chunks ;
std::vector<int> df_nrows ;
int k=0 ;
for( int i=0; i<ndata; i++) {
SEXP obj = dots[i] ;
if( Rf_isNull(obj) ) continue ;
chunks.push_back( obj ) ;
int nrows = chunks[k].nrows() ;
df_nrows.push_back(nrows) ;
n += nrows ;
k++ ;
}
ndata = chunks.size() ;
pointer_vector<Collecter> columns ;
std::vector<String> names ;
k=0 ;
Function enc2native( "enc2native" ) ;
for( int i=0; i<ndata; i++){
Rcpp::checkUserInterrupt() ;
const DataFrameAble& df = chunks[i] ;
if( !df.size() ) continue ;
int nrows = df.nrows() ;
CharacterVector df_names = enc2native(df.names()) ;
for( int j=0; j<df.size(); j++){
SEXP source = df.get(j) ;
String name = df_names[j] ;
Collecter* coll = 0;
size_t index = 0 ;
for( ; index < names.size(); index++){
if( name == names[index] ){
coll = columns[index] ;
break ;
}
}
if( ! coll ){
coll = collecter( source, n ) ;
columns.push_back( coll );
names.push_back(name) ;
}
if( coll->compatible(source) ){
// if the current source is compatible, collect
coll->collect( SlicingIndex( k, nrows), source ) ;
} else if( coll->can_promote(source) ) {
// setup a new Collecter
Collecter* new_collecter = promote_collecter(source, n, coll ) ;
// import data from this chunk
new_collecter->collect( SlicingIndex( k, nrows), source ) ;
// import data from previous collecter
new_collecter->collect( SlicingIndex(0, k), coll->get() ) ;
// dispose the previous collecter and keep the new one.
delete coll ;
columns[index] = new_collecter ;
} else if( all_na(source) ) {
// do nothing, the collecter already initialized data with the
// right NA
} else if( coll->is_logical_all_na() ) {
Collecter* new_collecter = collecter( source, n ) ;
new_collecter->collect( SlicingIndex(k, nrows), source ) ;
delete coll ;
columns[index] = new_collecter ;
} else {
std::string column_name(name) ;
stop( "incompatible type (data index: %d, column: '%s', was collecting: %s (%s), incompatible with data of type: %s",
(i+1), column_name, coll->describe(), DEMANGLE(*coll), get_single_class(source) );
}
}
k += nrows ;
}
int nc = columns.size() ;
int has_id = Rf_isNull(id) ? 0 : 1;
List out(nc + has_id) ;
CharacterVector out_names(nc + has_id) ;
for( int i=0; i<nc; i++){
out[i + has_id] = columns[i]->get() ;
out_names[i + has_id] = names[i] ;
}
// Add vector of identifiers if .id is supplied
if (!Rf_isNull(id)) {
CharacterVector df_names = dots.names() ;
CharacterVector id_col = no_init(n) ;
CharacterVector::iterator it = id_col.begin() ;
for (int i=0; i<ndata; ++i) {
std::fill( it, it + df_nrows[i], df_names[i] ) ;
it += df_nrows[i] ;
}
out[0] = id_col ;
out_names[0] = Rcpp::as<std::string>(id) ;
}
out.attr( "names" ) = out_names ;
set_rownames( out, n ) ;
out.attr( "class" ) = classes_not_grouped() ;
return out ;
}
// [[Rcpp::export]]
List bind_rows_( List dots, SEXP id = R_NilValue ){
return rbind__impl(dots, id) ;
}
// [[Rcpp::export]]
List rbind_list__impl( Dots dots ){
return rbind__impl(dots) ;
}
template <typename Dots>
List cbind__impl( Dots dots ){
int n = dots.size() ;
DataFrameAbleVector chunks ;
for( int i=0; i<n; i++) {
SEXP obj = dots[i] ;
if( !Rf_isNull(obj) )
chunks.push_back( dots[i] );
}
n = chunks.size() ;
// first check that the number of rows is the same
const DataFrameAble& df = chunks[0] ;
int nrows = df.nrows() ;
int nv = df.size() ;
for( int i=1; i<n; i++){
const DataFrameAble& current = chunks[i] ;
if( current.nrows() != nrows ){
stop( "incompatible number of rows (%d, expecting %d)", current.nrows(), nrows ) ;
}
nv += current.size() ;
}
// collect columns
List out(nv) ;
CharacterVector out_names(nv) ;
// then do the subsequent dfs
for( int i=0, k=0 ; i<n; i++){
Rcpp::checkUserInterrupt() ;
const DataFrameAble& current = chunks[i] ;
CharacterVector current_names = current.names() ;
int nc = current.size() ;
for( int j=0; j<nc; j++, k++){
out[k] = shared_SEXP(current.get(j)) ;
out_names[k] = current_names[j] ;
}
}
out.names() = out_names ;
set_rownames( out, nrows ) ;
out.attr( "class" ) = classes_not_grouped() ;
return out ;
}
// [[Rcpp::export]]
List cbind_all( List dots ){
return cbind__impl( dots ) ;
}
// [[Rcpp::export]]
SEXP combine_all( List data ){
int nv = data.size() ;
if( nv == 0 ) stop("combine_all needs at least one vector") ;
// get the size of the output
int n = 0 ;
for( int i=0; i<nv; i++){
n += Rf_length(data[i]) ;
}
// collect
boost::scoped_ptr<Collecter> coll( collecter( data[0], n ) ) ;
coll->collect( SlicingIndex(0, Rf_length(data[0])), data[0] ) ;
int k = Rf_length(data[0]) ;
for( int i=1; i<nv; i++){
SEXP current = data[i] ;
int n_current= Rf_length(current) ;
if( coll->compatible(current) ){
coll->collect( SlicingIndex(k, n_current), current ) ;
} else if( coll->can_promote(current) ) {
Collecter* new_coll = promote_collecter(current, n, coll.get() ) ;
new_coll->collect( SlicingIndex(k, n_current), current ) ;
new_coll->collect( SlicingIndex(0, k), coll->get() ) ;
coll.reset( new_coll ) ;
} else {
stop( "incompatible type at index %d : %s, was collecting : %s",
(i+1), get_single_class(current), get_single_class(coll->get()) ) ;
}
k += n_current ;
}
RObject out = coll->get() ;
return out ;
}