1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // Vector.cpp: Rcpp R/C++ interface class library -- Vector unit tests
4 //
5 // Copyright (C) 2012 - 2018 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21
22 #include <climits>
23 #include <Rcpp.h>
24 #include <sstream>
25
26 using namespace Rcpp ;
27
square(double x)28 inline double square( double x){ return x*x; }
29
30 // [[Rcpp::export]]
raw_()31 RawVector raw_(){
32 RawVector x(10) ;
33 for( int i=0; i<10; i++) x[i] = (Rbyte)i ;
34 return x ;
35 }
36
37 // [[Rcpp::export]]
raw_REALSXP(RawVector x)38 RawVector raw_REALSXP( RawVector x ){
39 for( int i=0; i<x.size(); i++) {
40 x[i] = x[i]*2 ;
41 }
42 return x ;
43 }
44
45 // [[Rcpp::export]]
expression_()46 ExpressionVector expression_(){
47 ExpressionVector x(2) ;
48 x[0] = Symbol( "rnorm" ) ;
49 x[1] = Rf_lcons( Symbol("rnorm"), Rf_cons( Rf_ScalarReal(10.0), R_NilValue) ) ;
50 return x ;
51 }
52
53 // [[Rcpp::export]]
expression_variadic()54 ExpressionVector expression_variadic(){
55 ExpressionVector x(2) ;
56 x[0] = Symbol( "rnorm" ) ;
57 x[1] = Language( "rnorm", 10.0 ) ;
58 return x ;
59 }
60
61 // [[Rcpp::export]]
expression_parse()62 ExpressionVector expression_parse(){
63 ExpressionVector code( "local( { y <- sample(1:10); sort(y) })" ) ;
64 return code ;
65 }
66
67 // [[Rcpp::export]]
expression_parseerror()68 ExpressionVector expression_parseerror(){
69 ExpressionVector code( "rnorm(" ) ;
70 return code ;
71 }
72
73 // [[Rcpp::export]]
expression_eval()74 SEXP expression_eval(){
75 ExpressionVector code( "local( { y <- sample(1:10); sort(y) })" ) ;
76 return code.eval() ;
77 }
78
79 // [[Rcpp::export]]
expression_evalenv(Environment env)80 SEXP expression_evalenv( Environment env){
81 ExpressionVector code( "sort(x)" ) ;
82 return code.eval(env) ;
83 }
84
85 // [[Rcpp::export]]
complex_()86 ComplexVector complex_(){
87 ComplexVector x(10) ;
88 Rcomplex rc ;
89 for( int i=0; i<10; i++) {
90 rc.r = rc.i = i + 0.0 ;
91 x[i] = rc ;
92 }
93 return x ;
94 }
95
96 // [[Rcpp::export]]
complex_CPLXSXP(ComplexVector x)97 ComplexVector complex_CPLXSXP( ComplexVector x ){
98 int nn = x.size();
99 for( int i=0; i<nn; i++) {
100 x[i].r = x[i].r*2 ;
101 x[i].i = x[i].i*2 ;
102 }
103 return x ;
104 }
105
106 // [[Rcpp::export]]
complex_INTSXP(SEXP vec)107 ComplexVector complex_INTSXP( SEXP vec ){
108 ComplexVector x(vec);
109 int nn = x.size();
110 IntegerVector tmp(nn, 2.0);
111 ComplexVector tmp1(tmp);
112 x = x * tmp1;
113 return x ;
114 }
115
116 // [[Rcpp::export]]
complex_REALSXP(SEXP vec)117 ComplexVector complex_REALSXP(SEXP vec){
118 ComplexVector x(vec);
119 int nn = x.size();
120 NumericVector tmp(nn, 3.0);
121 ComplexVector tmp1(tmp);
122 x = x * tmp1;
123 return x ;
124 }
125
126 // [[Rcpp::export]]
integer_ctor()127 IntegerVector integer_ctor(){
128 IntegerVector x(10) ;
129 for( int i=0; i<10; i++) x[i] = i ;
130 return x ;
131 }
132
133 // [[Rcpp::export]]
integer_INTSXP(SEXP vec)134 IntegerVector integer_INTSXP(SEXP vec){
135 IntegerVector x(vec) ;
136 for( int i=0; i<x.size(); i++) {
137 x[i] = x[i]*2 ;
138 }
139 return x ;
140 }
141
142 // [[Rcpp::export]]
integer_dimension_ctor_1()143 IntegerVector integer_dimension_ctor_1(){
144 return IntegerVector( Dimension( 5 ) ) ;
145 }
146
147 // [[Rcpp::export]]
integer_dimension_ctor_2()148 IntegerVector integer_dimension_ctor_2(){
149 return IntegerVector( Dimension( 5, 5 ) ) ;
150 }
151
152 // [[Rcpp::export]]
integer_dimension_ctor_3()153 IntegerVector integer_dimension_ctor_3(){
154 return IntegerVector( Dimension( 2, 3, 4) ) ;
155 }
156
157 // [[Rcpp::export]]
integer_range_ctor_1()158 IntegerVector integer_range_ctor_1(){
159 int x[] = { 0, 1, 2, 3 } ;
160 IntegerVector y( x, x+4 ) ;
161 return y;
162 }
163
164 // [[Rcpp::export]]
integer_range_ctor_2()165 IntegerVector integer_range_ctor_2(){
166 std::vector<int> vec(4) ;
167 for( size_t i = 0; i<4; i++) vec[i] = i;
168 IntegerVector y( vec.begin(), vec.end() ) ;
169 return y;
170 }
171
172 // [[Rcpp::export]]
integer_names_set()173 IntegerVector integer_names_set(){
174 IntegerVector y(2) ;
175 std::vector<std::string> names(2) ;
176 names[0] = "foo" ;
177 names[1] = "bar" ;
178 y.names() = names ;
179 return y ;
180 }
181
182 // [[Rcpp::export]]
integer_names_get(IntegerVector y)183 CharacterVector integer_names_get( IntegerVector y ){
184 return y.names() ;
185 }
186
187 // [[Rcpp::export]]
integer_names_indexing(IntegerVector y)188 int integer_names_indexing( IntegerVector y ){
189 return y["foo"] ;
190 }
191
192 // [[Rcpp::export]]
integer_push_back(IntegerVector y)193 IntegerVector integer_push_back( IntegerVector y ){
194 y.push_back( 5 ) ;
195 return y ;
196 }
197
198 // [[Rcpp::export]]
integer_push_front(IntegerVector y)199 IntegerVector integer_push_front( IntegerVector y ){
200 y.push_front( 5 ) ;
201 return y ;
202 }
203
204 // [[Rcpp::export]]
integer_insert(IntegerVector y)205 IntegerVector integer_insert( IntegerVector y){
206 y.insert( 0, 5 ) ;
207 y.insert( 2, 7 ) ;
208 return y ;
209 }
210
211 // [[Rcpp::export]]
integer_erase(IntegerVector y)212 IntegerVector integer_erase( IntegerVector y ){
213 y.erase(2) ;
214 return y ;
215 }
216
217 // [[Rcpp::export]]
integer_erase_range(IntegerVector x,IntegerVector y)218 List integer_erase_range( IntegerVector x, IntegerVector y ){
219 x.erase(x.begin()+5, x.end()-1 );
220 y.erase(y.begin()+5, y.end()-1 );
221 return List::create( x, y ) ;
222 }
223
224 // [[Rcpp::export]]
integer_erase_range_2(IntegerVector x,IntegerVector y)225 List integer_erase_range_2( IntegerVector x, IntegerVector y ){
226 IntegerVector::iterator it = x.begin()+1 ;
227 while( it != x.end() ){
228 it = x.erase(it) ;
229 }
230 it = y.begin() + 1 ;
231 while( it != y.end() ){
232 it = y.erase(it) ;
233 }
234 return List::create( x, y ) ;
235 }
236
237 // [[Rcpp::export]]
List_erase_range_2(List x,List y)238 List List_erase_range_2( List x, List y ){
239 List::iterator it = x.begin()+1 ;
240 while( it != x.end() ){
241 it = x.erase(it) ;
242 }
243 it = y.begin() + 1 ;
244 while( it != y.end() ){
245 it = y.erase(it) ;
246 }
247 return List::create( x, y ) ;
248 }
249
250
251 // [[Rcpp::export]]
integer_erase2(IntegerVector y)252 IntegerVector integer_erase2( IntegerVector y ){
253 y.erase(1,2) ;
254 return y ;
255 }
256
257 // [[Rcpp::export]]
integer_fill(IntegerVector y)258 IntegerVector integer_fill( IntegerVector y ){
259 y.fill(10) ;
260 return y ;
261 }
262
263 // [[Rcpp::export]]
integer_zero()264 IntegerVector integer_zero(){
265 return IntegerVector(0);
266 }
267
268 // [[Rcpp::export]]
integer_create_zero()269 IntegerVector integer_create_zero(){
270 return IntegerVector::create();
271 }
272
273 // [[Rcpp::export]]
integer_create_()274 List integer_create_(){
275 List output(2);
276 output[0] = IntegerVector::create( 10, 20 ) ;
277 output[1] = IntegerVector::create(
278 _["foo"] = 20,
279 _["bar"] = 30 ) ;
280 return output ;
281 }
282
283 // [[Rcpp::export]]
integer_clone_(IntegerVector vec)284 IntegerVector integer_clone_( IntegerVector vec ){
285 IntegerVector dolly = clone( vec ) ;
286 for( size_t i=0; i<10; i++){
287 dolly[i] = 10 - i ;
288 }
289 return dolly ;
290 }
291
292 // [[Rcpp::export]]
numeric_()293 NumericVector numeric_(){
294 NumericVector x(10) ;
295 for( int i=0; i<10; i++) x[i] = i ;
296 return x ;
297 }
298
299 // [[Rcpp::export]]
numeric_REALSXP(SEXP vec)300 NumericVector numeric_REALSXP( SEXP vec){
301 NumericVector x(vec) ;
302 for( int i=0; i<x.size(); i++) {
303 x[i] = x[i]*2.0 ;
304 }
305 return x ;
306 }
307
308 // [[Rcpp::export]]
numeric_import()309 IntegerVector numeric_import(){
310 std::vector<int> v(10) ;
311 for( int i=0; i<10; i++) v[i] = i ;
312 return IntegerVector::import( v.begin(), v.end() ) ;
313 }
314
315 // [[Rcpp::export]]
numeric_importtransform()316 NumericVector numeric_importtransform(){
317 std::vector<double> v(10) ;
318 for( int i=0; i<10; i++) v[i] = i ;
319
320 return NumericVector::import_transform( v.begin(), v.end(), square ) ;
321 }
322
323 // [[Rcpp::export]]
list_ctor()324 List list_ctor(){
325 List x(10) ;
326 for( int i=0; i<10; i++) x[i] = Rf_ScalarInteger( i * 2) ;
327 return x ;
328 }
329
330 // [[Rcpp::export]]
list_template_()331 List list_template_(){
332 List x(4) ;
333 x[0] = "foo" ;
334 x[1] = 10 ;
335 x[2] = 10.2 ;
336 x[3] = false;
337 return x ;
338 }
339
340 // [[Rcpp::export]]
list_VECSXP_(SEXP vec)341 List list_VECSXP_( SEXP vec){
342 List x(vec) ;
343 return x ;
344 }
345
346 // [[Rcpp::export]]
list_matrix_indexing_1(List m)347 List list_matrix_indexing_1( List m ){
348 List out(4) ;
349 for( size_t i=0 ; i<4; i++){
350 out[i] = m(i,i) ;
351 }
352 return out ;
353 }
354
355 // [[Rcpp::export]]
list_matrix_indexing_2(GenericVector m)356 List list_matrix_indexing_2( GenericVector m ){
357 for(size_t i=0 ; i<4; i++){
358 m(i,i) = "foo" ;
359 }
360 return m ;
361 }
362
363 // [[Rcpp::export]]
list_Dimension_constructor_1()364 List list_Dimension_constructor_1(){
365 return List( Dimension( 5 ) ) ;
366 }
367
368 // [[Rcpp::export]]
list_Dimension_constructor_2()369 List list_Dimension_constructor_2(){
370 return List( Dimension( 5, 5 ) );
371 }
372
373 // [[Rcpp::export]]
list_Dimension_constructor_3()374 List list_Dimension_constructor_3(){
375 return List( Dimension( 2, 3, 4) ) ;
376 }
377
378 // [[Rcpp::export]]
list_iterator_(List input,Function fun)379 List list_iterator_( List input, Function fun){
380 List output( input.size() ) ;
381 std::transform( input.begin(), input.end(), output.begin(), fun ) ;
382 output.names() = input.names() ;
383 return output ;
384 }
385
386 // [[Rcpp::export]]
list_name_indexing(List df)387 int list_name_indexing( List df ){
388 IntegerVector df_x = df["x"] ;
389 int res = std::accumulate( df_x.begin(), df_x.end(), 0 ) ;
390 return res ;
391 }
392
393 // [[Rcpp::export]]
list_push_back(List list)394 List list_push_back(List list){
395 list.push_back( 10 ) ;
396 list.push_back( "bar", "foo" ) ;
397 return list ;
398 }
399
400 // [[Rcpp::export]]
list_push_front(List list)401 List list_push_front( List list ){
402 list.push_front( 10 ) ;
403 list.push_front( "bar", "foo" ) ;
404 return list ;
405 }
406
407 // [[Rcpp::export]]
list_erase(List list)408 List list_erase( List list ){
409 list.erase( list.begin() ) ;
410 return list ;
411 }
412
413 // [[Rcpp::export]]
list_erase_range(List list)414 List list_erase_range( List list ){
415 list.erase( 0, 2 ) ;
416 return list ;
417 }
418
419 // [[Rcpp::export]]
list_implicit_push_back()420 List list_implicit_push_back(){
421 List list ;
422 list["foo"] = 10 ;
423 list["bar" ] = "foobar" ;
424 return list ;
425 }
426
427 // [[Rcpp::export]]
list_create_()428 List list_create_(){
429 List output(2);
430 output[0] = List::create( 10, "foo" ) ;
431 output[1] = List::create(
432 _["foo"] = 10,
433 _["bar"] = true ) ;
434 return output ;
435 }
436
437 // [[Rcpp::export]]
list_stdcomplex()438 List list_stdcomplex(){
439 std::vector< std::complex<double> > v_double(10) ;
440 std::vector< std::complex<float> > v_float(10) ;
441 return List::create( _["float"] = v_float, _["double"] = v_double ) ;
442 }
443
444 // [[Rcpp::export]]
character_ctor()445 CharacterVector character_ctor(){
446 CharacterVector x(10) ;
447 for( int i=0; i<10; i++) x[i] = "foo" ;
448 return x ;
449 }
450
451 // [[Rcpp::export]]
character_STRSXP_(SEXP vec)452 std::string character_STRSXP_( SEXP vec ){
453 CharacterVector x(vec) ;
454 std::string st = "" ;
455 for( int i=0; i<x.size(); i++) {
456 st += x[i] ;
457 }
458 return st ;
459 }
460
461 // [[Rcpp::export]]
character_plusequals()462 CharacterVector character_plusequals(){
463 CharacterVector x(2) ;
464 x[0] = "foo" ;
465 x[1] = "bar" ;
466 x[0] += "bar" ;
467 x[1] += x[0] ;
468 return x ;
469 }
470
471 // [[Rcpp::export]]
character_matrix_indexing(CharacterVector m)472 CharacterVector character_matrix_indexing( CharacterVector m ){
473 std::string trace;
474 for( size_t i=0 ; i<4; i++){
475 trace += m(i,i) ;
476 }
477 return wrap( trace ) ;
478 }
479
480 // [[Rcpp::export]]
character_matrix_indexing_lhs(CharacterVector m)481 CharacterVector character_matrix_indexing_lhs( CharacterVector m ){
482 for( size_t i=0 ; i<4; i++){
483 m(i,i) = "foo" ;
484 }
485 return m ;
486 }
487
488 // [[Rcpp::export]]
character_matrix_row_iteration_incr(CharacterMatrix m)489 CharacterVector character_matrix_row_iteration_incr( CharacterMatrix m ){
490 std::string pasted_row;
491 CharacterMatrix::Row row(m(1, _));
492 CharacterMatrix::Row::iterator i_row(row.begin());
493 for( size_t i=0 ; i<4; i++){
494 pasted_row += *i_row++;
495 }
496 return wrap( pasted_row ) ;
497 }
498
499 // [[Rcpp::export]]
character_matrix_row_iteration_decr(CharacterMatrix m)500 CharacterVector character_matrix_row_iteration_decr( CharacterMatrix m ){
501 std::string pasted_row;
502 CharacterMatrix::Row row(m(1, _));
503 CharacterMatrix::Row::iterator i_row(row.end());
504 i_row--; // Step back from 'one past the end' to 'last element'.
505 // Only copy the last three elements, to avoid creating an invalid
506 // 'one before the beginning' iterator:
507 for( size_t i=0 ; i<3; i++){
508 pasted_row += *i_row--;
509 }
510 return wrap( pasted_row ) ;
511 }
512
513
514 // [[Rcpp::export]]
character_assign1()515 CharacterVector character_assign1(){
516 const char* x[] = { "foo", "bar", "bling", "boom" } ;
517 CharacterVector y ;
518 y.assign( x, x+4 ) ;
519 return y;
520 }
521
522 // [[Rcpp::export]]
character_assign2()523 CharacterVector character_assign2(){
524 std::vector<std::string> vec(4) ;
525 vec[0] = "foo";
526 vec[1] = "bar";
527 vec[2] = "bling";
528 vec[3] = "boom" ;
529 CharacterVector y ;
530 y.assign( vec.begin(), vec.end() ) ;
531 return y;
532 }
533
534 // [[Rcpp::export]]
character_range_ctor1()535 CharacterVector character_range_ctor1(){
536 const char* x[] = { "foo", "bar", "bling", "boom" } ;
537 CharacterVector y( x, x+4 ) ;
538 return y;
539 }
540
541 // [[Rcpp::export]]
character_range_ctor2()542 CharacterVector character_range_ctor2(){
543 std::vector<std::string> vec(4) ;
544 vec[0] = "foo";
545 vec[1] = "bar";
546 vec[2] = "bling";
547 vec[3] = "boom" ;
548 CharacterVector y( vec.begin(), vec.end() ) ;
549 return y;
550 }
551
552 // [[Rcpp::export]]
character_dimension_ctor1()553 CharacterVector character_dimension_ctor1(){
554 return CharacterVector( Dimension( 5 ) ) ;
555 }
556
557 // [[Rcpp::export]]
character_dimension_ctor2()558 CharacterVector character_dimension_ctor2(){
559 return CharacterVector( Dimension( 5, 5 ) ) ;
560 }
561
562 // [[Rcpp::export]]
character_dimension_ctor3()563 CharacterVector character_dimension_ctor3(){
564 return CharacterVector( Dimension( 2, 3, 4) ) ;
565 }
566
567 // [[Rcpp::export]]
character_iterator1(CharacterVector letters)568 std::string character_iterator1( CharacterVector letters ){
569 std::string res ;
570 CharacterVector::iterator first = letters.begin() ;
571 CharacterVector::iterator last = letters.end() ;
572 while( first != last ){
573 res += *first ;
574 ++first ;
575 }
576 return res ;
577 }
578
579 // [[Rcpp::export]]
character_const_iterator1(const CharacterVector letters)580 std::string character_const_iterator1( const CharacterVector letters ){
581 std::string res ;
582 CharacterVector::const_iterator first = letters.begin() ;
583 CharacterVector::const_iterator last = letters.end() ;
584 while( first != last ){
585 res += *first ;
586 ++first ;
587 }
588 return res ;
589 }
590
591
592 // [[Rcpp::export]]
character_iterator2(CharacterVector letters)593 std::string character_iterator2( CharacterVector letters ){
594 std::string res(std::accumulate(letters.begin(), letters.end(), std::string()));
595 return res ;
596 }
597
598 // [[Rcpp::export]]
character_const_iterator2(const CharacterVector letters)599 std::string character_const_iterator2( const CharacterVector letters ){
600 std::string res(std::accumulate(letters.begin(), letters.end(), std::string()));
601 return res ;
602 }
603
604 // [[Rcpp::export]]
character_reverse(CharacterVector y)605 CharacterVector character_reverse( CharacterVector y ){
606 std::reverse( y.begin(), y.end() ) ;
607 return y ;
608 }
609
610 // [[Rcpp::export]]
character_names_indexing(CharacterVector y)611 std::string character_names_indexing( CharacterVector y ){
612 std::string foo( y["foo"] ) ;
613 return foo ;
614 }
615
616 // [[Rcpp::export]]
character_listOf(List ll)617 List character_listOf( List ll ){
618 CharacterVector cv1 = ll["foo"];
619 CharacterVector cv2 = ll["bar"];
620 std::string rv1 = std::string(cv1[0]) + cv1[1] + cv1[2];
621 std::string rv2 = std::string(cv2[0]) + cv2[1] + cv2[2];
622 return List::create(_["foo"] = rv1, _["bar"] = rv2);
623 }
624
625 // [[Rcpp::export]]
character_find_(CharacterVector y)626 int character_find_(CharacterVector y){
627 CharacterVector::iterator it = std::find( y.begin(), y.end(), "foo" ) ;
628 return std::distance( y.begin(), it );
629 }
630
631 // [[Rcpp::export]]
character_create_()632 List character_create_(){
633 List output(2);
634 output[0] = CharacterVector::create( "foo", "bar" ) ;
635 output[1] = CharacterVector::create(
636 _["foo"] = "bar",
637 _["bar"] = "foo"
638 ) ;
639 return output ;
640 }
641
642 // [[Rcpp::export]]
complex_binary_sugar(ComplexVector xx,ComplexVector yy)643 List complex_binary_sugar(ComplexVector xx, ComplexVector yy){
644 return List::create(
645 _["+"] = xx + yy,
646 _["-"] = xx - yy,
647 _["*"] = xx * yy,
648 _["/"] = xx / yy
649 ) ;
650 }
651
652 // [[Rcpp::export]]
List_extract(List input)653 List List_extract( List input ){
654 bool a = input[0] ;
655 int b = input[1] ;
656 return List::create(a, b) ;
657 }
658
659 // [[Rcpp::export]]
factors(CharacterVector s)660 CharacterVector factors( CharacterVector s){
661 return s;
662 }
663
664 // [[Rcpp::export]]
IntegerVector_int_init()665 IntegerVector IntegerVector_int_init(){
666 IntegerVector x(2,4) ;
667 return x ;
668 }
669
670 // [[Rcpp::export]]
containsElementNamed(List l,CharacterVector n)671 bool containsElementNamed( List l, CharacterVector n){
672 return l.containsElementNamed(n[0]);
673 }
674
675 // [[Rcpp::export]]
CharacterVectorEqualityOperator(CharacterVector x,CharacterVector y)676 List CharacterVectorEqualityOperator( CharacterVector x, CharacterVector y){
677 int n = x.size() ;
678 LogicalVector eq(n), neq(n);
679 for( int i=0; i<n; i++){
680 eq[i] = x[i] == y[i] ;
681 neq[i] = x[i] != y[i] ;
682 }
683 return List::create(eq, neq) ;
684 }
685
686 // [[Rcpp::export]]
List_rep_ctor(IntegerVector x)687 List List_rep_ctor(IntegerVector x){
688 return List(3, x) ;
689 }
690
691 // [[Rcpp::export]]
stdVectorDouble(std::vector<double> x)692 int stdVectorDouble(std::vector<double> x) {
693 return x.size();
694 }
695
696 // [[Rcpp::export]]
stdVectorDoubleConst(const std::vector<double> x)697 int stdVectorDoubleConst(const std::vector<double> x) {
698 return x.size();
699 }
700
701 // [[Rcpp::export]]
stdVectorDoubleRef(std::vector<double> & x)702 int stdVectorDoubleRef(std::vector<double> & x) {
703 return x.size();
704 }
705
706 // [[Rcpp::export]]
stdVectorDoubleConstRef(const std::vector<double> & x)707 int stdVectorDoubleConstRef(const std::vector<double> & x) {
708 return x.size();
709 }
710
711 // [[Rcpp::export]]
stdVectorInt(std::vector<int> x)712 int stdVectorInt(std::vector<int> x) {
713 return x.size();
714 }
715
716 // [[Rcpp::export]]
stdVectorIntConst(const std::vector<int> x)717 int stdVectorIntConst(const std::vector<int> x) {
718 return x.size();
719 }
720
721 // [[Rcpp::export]]
stdVectorIntRef(std::vector<int> & x)722 int stdVectorIntRef(std::vector<int> & x) {
723 return x.size();
724 }
725
726 // [[Rcpp::export]]
stdVectorIntConstRef(const std::vector<int> & x)727 int stdVectorIntConstRef(const std::vector<int> & x) {
728 return x.size();
729 }
730
731 // [[Rcpp::export]]
character_vector_const_proxy(const CharacterVector & str)732 std::string character_vector_const_proxy(const CharacterVector& str){
733 char* cstr = (char*) str[0] ;
734 std::string res ;
735 res += cstr ;
736 return cstr ;
737 }
738
739 // [[Rcpp::export]]
CharacterVector_test_const_proxy(const CharacterVector x)740 CharacterVector CharacterVector_test_const_proxy(const CharacterVector x){
741 CharacterVector out( x.size() ) ;
742 for( int i=0; i<x.size(); i++){
743 out[i] = x[i] ;
744 }
745 return out ;
746 }
747
748 // [[Rcpp::export]]
sort_numeric(NumericVector x)749 NumericVector sort_numeric(NumericVector x) {
750 return x.sort();
751 }
752
753 // [[Rcpp::export]]
sort_integer(IntegerVector x)754 IntegerVector sort_integer(IntegerVector x) {
755 return x.sort();
756 }
757
758 // [[Rcpp::export]]
sort_character(CharacterVector x)759 CharacterVector sort_character(CharacterVector x) {
760 return x.sort();
761 }
762
763 // [[Rcpp::export]]
sort_logical(LogicalVector x)764 LogicalVector sort_logical(LogicalVector x) {
765 return x.sort();
766 }
767
768 // [[Rcpp::export]]
sort_numeric_desc(NumericVector x)769 NumericVector sort_numeric_desc(NumericVector x) {
770 return x.sort(true);
771 }
772
773 // [[Rcpp::export]]
sort_integer_desc(IntegerVector x)774 IntegerVector sort_integer_desc(IntegerVector x) {
775 return x.sort(true);
776 }
777
778 // [[Rcpp::export]]
sort_character_desc(CharacterVector x)779 CharacterVector sort_character_desc(CharacterVector x) {
780 return x.sort(true);
781 }
782
783 // [[Rcpp::export]]
sort_logical_desc(LogicalVector x)784 LogicalVector sort_logical_desc(LogicalVector x) {
785 return x.sort(true);
786 }
787
788 // [[Rcpp::export]]
list_sexp_assign(SEXP x)789 List list_sexp_assign(SEXP x) {
790 List L;
791 L = x;
792 return L;
793 }
794
795 // [[Rcpp::export]]
logical_vector_from_bool()796 bool logical_vector_from_bool() {
797 return true;
798 }
799
800 // [[Rcpp::export]]
logical_vector_from_bool_assign()801 LogicalVector logical_vector_from_bool_assign() {
802 LogicalVector result = true;
803 return result;
804 }
805
806 // [[Rcpp::export]]
no_op(int major)807 void no_op(int major) {
808 //int minor = 1;
809 }
810
811 // [[Rcpp::export]]
noprotect_vector(Vector<REALSXP,NoProtectStorage> x)812 int noprotect_vector( Vector<REALSXP, NoProtectStorage> x){
813 return x.size() ;
814 }
815
816 // [[Rcpp::export]]
noprotect_matrix(Matrix<REALSXP,NoProtectStorage> x)817 int noprotect_matrix( Matrix<REALSXP, NoProtectStorage> x){
818 return x.nrow() ;
819 }
820
821 // [[Rcpp::export]]
vec_access_with_bounds_checking(const IntegerVector x,int index)822 int vec_access_with_bounds_checking(const IntegerVector x, int index) {
823 return x.at(index);
824 }
825
826 // [[Rcpp::export]]
vec_print_numeric(NumericVector v)827 String vec_print_numeric(NumericVector v) {
828 std::ostringstream buf;
829 buf << v;
830 return buf.str();
831 }
832
833 // [[Rcpp::export]]
vec_print_character(CharacterVector v)834 String vec_print_character(CharacterVector v) {
835 std::ostringstream buf;
836 buf << v;
837 return buf.str();
838 }
839
840 // [[Rcpp::export]]
vec_print_integer(IntegerVector v)841 String vec_print_integer(IntegerVector v) {
842 std::ostringstream buf;
843 buf << v;
844 return buf.str();
845 }
846
847 // [[Rcpp::export]]
vec_subset(IntegerVector x,IntegerVector y)848 IntegerVector vec_subset(IntegerVector x, IntegerVector y) {
849 return x[y - 1];
850 }
851
852 // [[Rcpp::export]]
CharacterVectorNoProtect(Vector<STRSXP,NoProtectStorage> s)853 int CharacterVectorNoProtect(Vector<STRSXP, NoProtectStorage> s){
854 s[0] = "";
855 return s.size();
856 }
857
858 // [[Rcpp::export]]
CharacterVectorNoProtect_crosspolicy(Vector<STRSXP,NoProtectStorage> s)859 CharacterVector CharacterVectorNoProtect_crosspolicy(Vector<STRSXP, NoProtectStorage> s){
860 CharacterVector s2(1) ;
861 s2[0] = s[0];
862 return s;
863 }
864
865 // [[Rcpp::export]]
ListNoProtect_crosspolicy(Vector<VECSXP,NoProtectStorage> data)866 List ListNoProtect_crosspolicy(Vector<VECSXP, NoProtectStorage> data){
867 List data2(1) ;
868 data2[0] = data[0];
869 return data2;
870 }
871
872 // [[Rcpp::export]]
CharacterVector_test_equality(CharacterVector x,CharacterVector y)873 bool CharacterVector_test_equality(CharacterVector x, CharacterVector y) {
874 if (x.length() != y.length()) {
875 return false;
876 }
877
878 return std::equal(x.begin(), x.end(), y.begin());
879 }
880
881 // [[Rcpp::export]]
CharacterVector_test_equality_crosspolicy(CharacterVector x,Vector<STRSXP,NoProtectStorage> y)882 bool CharacterVector_test_equality_crosspolicy(CharacterVector x, Vector<STRSXP,NoProtectStorage> y) {
883 if (x.length() != y.length()) {
884 return false;
885 }
886
887 return std::equal(x.begin(), x.end(), y.begin());
888 }
889