-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.cpp
More file actions
434 lines (370 loc) · 8.99 KB
/
Vector.cpp
File metadata and controls
434 lines (370 loc) · 8.99 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
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
#include <Rcpp.h>
using namespace Rcpp ;
inline double square( double x){ return x*x; }
// [[Rcpp::export]]
RawVector raw_(){
RawVector x(10) ;
for( int i=0; i<10; i++) x[i] = (Rbyte)i ;
return x ;
}
// [[Rcpp::export]]
RawVector raw_REALSXP( RawVector x ){
for( int i=0; i<x.size(); i++) {
x[i] = x[i]*2 ;
}
return x ;
}
// [[Rcpp::export]]
ExpressionVector expression_(){
ExpressionVector x(2) ;
x[0] = Symbol( "rnorm" ) ;
x[1] = Rf_lcons( Symbol("rnorm"), Rf_cons( Rf_ScalarReal(10.0), R_NilValue) ) ;
return x ;
}
// [[Rcpp::export]]
ExpressionVector expression_variadic(){
ExpressionVector x(2) ;
x[0] = Symbol( "rnorm" ) ;
x[1] = Language( "rnorm", 10.0 ) ;
return x ;
}
// [[Rcpp::export]]
ExpressionVector expression_parse(){
ExpressionVector code( "local( { y <- sample(1:10); sort(y) })" ) ;
return code ;
}
// [[Rcpp::export]]
ExpressionVector expression_parseerror(){
ExpressionVector code( "rnorm(" ) ;
return code ;
}
// [[Rcpp::export]]
ComplexVector complex_(){
ComplexVector x(10) ;
Rcomplex rc ;
for( int i=0; i<10; i++) {
rc.r = rc.i = i + 0.0 ;
x[i] = rc ;
}
return x ;
}
// [[Rcpp::export]]
ComplexVector complex_CPLXSXP( ComplexVector x ){
int nn = x.size();
for( int i=0; i<nn; i++) {
x[i].r = x[i].r*2 ;
x[i].i = x[i].i*2 ;
}
return x ;
}
// [[Rcpp::export]]
ComplexVector complex_INTSXP( SEXP vec ){
ComplexVector x(vec);
int nn = x.size();
IntegerVector tmp(nn, 2.0);
ComplexVector tmp1(tmp);
x = x * tmp1;
return x ;
}
// [[Rcpp::export]]
ComplexVector complex_REALSXP(SEXP vec){
ComplexVector x(vec);
int nn = x.size();
NumericVector tmp(nn, 3.0);
ComplexVector tmp1(tmp);
x = x * tmp1;
return x ;
}
// [[Rcpp::export]]
IntegerVector integer_ctor(){
IntegerVector x(10) ;
for( int i=0; i<10; i++) x[i] = i ;
return x ;
}
// [[Rcpp::export]]
IntegerVector integer_INTSXP(SEXP vec){
IntegerVector x(vec) ;
for( int i=0; i<x.size(); i++) {
x[i] = x[i]*2 ;
}
return x ;
}
// [[Rcpp::export]]
IntegerVector integer_names_set(){
IntegerVector y(2) ;
std::vector<std::string> names_{"foo","bar"} ;
names(y) = names_ ;
return y ;
}
// [[Rcpp::export]]
CharacterVector integer_names_get( IntegerVector y ){
return names(y) ;
}
// [[Rcpp::export]]
int integer_names_indexing( IntegerVector y ){
return y["foo"] ;
}
// [[Rcpp::export]]
IntegerVector integer_zero(){
return IntegerVector(0);
}
// [[Rcpp::export]]
IntegerVector integer_create_zero(){
return IntegerVector::create();
}
// [[Rcpp::export]]
List integer_create_(){
List output(2);
output[0] = IntegerVector::create( 10, 20 ) ;
output[1] = IntegerVector::create(
_["foo"] = 20,
_["bar"] = 30 ) ;
return output ;
}
// [[Rcpp::export]]
IntegerVector integer_clone_( IntegerVector vec ){
IntegerVector dolly = clone( vec ) ;
for( size_t i=0; i<10; i++){
dolly[i] = 10 - i ;
}
return dolly ;
}
// [[Rcpp::export]]
NumericVector numeric_(){
NumericVector x(10) ;
for( int i=0; i<10; i++) x[i] = i ;
return x ;
}
// [[Rcpp::export]]
NumericVector numeric_REALSXP( SEXP vec){
NumericVector x(vec) ;
for( int i=0; i<x.size(); i++) {
x[i] = x[i]*2.0 ;
}
return x ;
}
// [[Rcpp::export]]
List list_ctor(){
List x(10) ;
for( int i=0; i<10; i++) x[i] = Rf_ScalarInteger( i * 2) ;
return x ;
}
// [[Rcpp::export]]
List list_template_(){
List x(4) ;
x[0] = "foo" ;
x[1] = 10 ;
x[2] = 10.2 ;
x[3] = false;
return x ;
}
// [[Rcpp::export]]
List list_VECSXP_( SEXP vec){
List x(vec) ;
return x ;
}
// [[Rcpp::export]]
List list_iterator_( List input, Function fun){
List output( input.size() ) ;
std::transform( input.begin(), input.end(), output.begin(), fun ) ;
names(output) = names(input) ;
return output ;
}
// [[Rcpp::export]]
int list_name_indexing( List df ){
IntegerVector df_x = df["x"] ;
int res = std::accumulate( df_x.begin(), df_x.end(), 0 ) ;
return res ;
}
// [[Rcpp::export]]
List list_implicit_push_back(){
List list ;
list["foo"] = 10 ;
list["bar" ] = "foobar" ;
return list ;
}
// [[Rcpp::export]]
List list_create_(){
List output(2);
output[0] = List::create( 10, "foo" ) ;
output[1] = List::create(
_["foo"] = 10,
_["bar"] = true ) ;
return output ;
}
// [[Rcpp::export]]
List list_stdcomplex(){
std::vector< std::complex<double> > v_double(10) ;
std::vector< std::complex<float> > v_float(10) ;
return List::create( _["float"] = v_float, _["double"] = v_double ) ;
}
// [[Rcpp::export]]
CharacterVector character_ctor(){
CharacterVector x(10) ;
for( int i=0; i<10; i++) x[i] = "foo" ;
return x ;
}
// [[Rcpp::export]]
std::string character_STRSXP_( SEXP vec ){
CharacterVector x(vec) ;
std::string st = "" ;
for( int i=0; i<x.size(); i++) {
st += x[i] ;
}
return st ;
}
// [[Rcpp::export]]
CharacterVector character_plusequals(){
CharacterVector x(2) ;
x[0] = "foo" ;
x[1] = "bar" ;
x[0] += "bar" ;
x[1] += x[0] ;
return x ;
}
// [[Rcpp::export]]
std::string character_iterator1( CharacterVector letters ){
std::string res ;
CharacterVector::iterator first = letters.begin() ;
CharacterVector::iterator last = letters.end() ;
while( first != last ){
res += *first ;
++first ;
}
return res ;
}
// [[Rcpp::export]]
std::string character_iterator2( CharacterVector letters ){
std::string res(std::accumulate(letters.begin(), letters.end(), std::string()));
return res ;
}
// [[Rcpp::export]]
CharacterVector character_reverse( CharacterVector y ){
std::reverse( y.begin(), y.end() ) ;
return y ;
}
// [[Rcpp::export]]
std::string character_names_indexing( CharacterVector y ){
std::string foo = y["foo"] ;
return foo ;
}
// [[Rcpp::export]]
int character_find_(CharacterVector y){
CharacterVector::iterator it = std::find( y.begin(), y.end(), "foo" ) ;
return std::distance( y.begin(), it );
}
// [[Rcpp::export]]
List character_create_(){
List output(2);
output[0] = CharacterVector::create( "foo", "bar" ) ;
output[1] = CharacterVector::create(
_["foo"] = "bar",
_["bar"] = "foo"
) ;
return output ;
}
// [[Rcpp::export]]
List List_extract( List input ){
bool a = input[0] ;
int b = input[1] ;
return List::create(a, b) ;
}
// [[Rcpp::export]]
CharacterVector factors( CharacterVector s){
return s;
}
// [[Rcpp::export]]
IntegerVector IntegerVector_int_init(){
IntegerVector x(2,4) ;
return x ;
}
// [[Rcpp::export]]
List CharacterVectorEqualityOperator( CharacterVector x, CharacterVector y){
int n = x.size() ;
LogicalVector eq(n), neq(n);
for( int i=0; i<n; i++){
eq[i] = x[i] == y[i] ;
neq[i] = x[i] != y[i] ;
}
return List::create(eq, neq) ;
}
// [[Rcpp::export]]
List List_rep_ctor(IntegerVector x){
return List(3, x) ;
}
// [[Rcpp::export]]
int stdVectorDouble(std::vector<double> x) {
return x.size();
}
// [[Rcpp::export]]
int stdVectorDoubleConst(const std::vector<double> x) {
return x.size();
}
// [[Rcpp::export]]
int stdVectorDoubleRef(std::vector<double> & x) {
return x.size();
}
// [[Rcpp::export]]
int stdVectorDoubleConstRef(const std::vector<double> & x) {
return x.size();
}
// [[Rcpp::export]]
int stdVectorInt(std::vector<int> x) {
return x.size();
}
// [[Rcpp::export]]
int stdVectorIntConst(const std::vector<int> x) {
return x.size();
}
// [[Rcpp::export]]
int stdVectorIntRef(std::vector<int> & x) {
return x.size();
}
// [[Rcpp::export]]
int stdVectorIntConstRef(const std::vector<int> & x) {
return x.size();
}
// [[Rcpp::export]]
std::string character_vector_const_proxy(const CharacterVector& str){
std::string res = str[0] ;
return res ;
}
// [[Rcpp::export]]
CharacterVector CharacterVector_test_const_proxy(const CharacterVector x){
CharacterVector out( x.size() ) ;
for( int i=0; i<x.size(); i++){
out[i] = x[i] ;
}
return out ;
}
// [[Rcpp::export]]
LogicalVector logical_vector_from_initializer_list() {
return LogicalVector{true, true, false};
}
template <typename T, typename U>
List compound( T x, U y){
T sum=clone(x), minus=clone(x), times=clone(x), divides = clone(x);
sum += y ;
minus -= y ;
times *= y ;
divides /= y ;
return list(sum, minus, times, divides) ;
}
// [[Rcpp::export]]
List compound_operators_Vector( NumericVector x, NumericVector y) {
return compound(x, y) ;
}
// [[Rcpp::export]]
List compound_operators_Vector_primitive( NumericVector x, double y){
return compound(x, y) ;
}
// [[Rcpp::export]]
List compound_operators_Matrix_primitive( NumericMatrix x, double y){
return compound(x, y) ;
}
// [[Rcpp::export]]
NumericVector test_large_vector() {
R_xlen_t nl = 5000000000;
NumericVector v(nl);
return v;
}