Hi,
There is a preliminary version of this in Rcpp11. For example:
#include <Rcpp.h>
using namespace Rcpp ;
// [[Rcpp::export]]
NumericVector test(){
NumericVector x(10) ;
NumericVector y(30) ;
return NumericVector::concat(x, 2.0, y, 1.0) ;
}
So it handles (or at least it should):
- compatible primitive (i.e. double in the case of NumericVector)
- arbitrary compatible sugar expression
It is at least for now a static member function of the Vector class. Maybe it
can be made a free function.
This is about 90 lines of C++11 code.
Romain
Le 22 janv. 2014 à 01:37, Søren Højsgaard <[email protected]> a écrit :
> Hi Kevin,
>
> Thanks for your reply. I was only introduced to C++11 last week (my fault!);
> it seems that everybodys life becomes much easier once R-packages can be made
> with that...
>
> I think many Rcpp-friends would welcome a version of c( ). One view is of
> course that it should be as general as R's c( )-function. A more pragmatic
> view is a version of c() that could concatenate (a) two lists and (b) two
> vectors (where each can be integers, doubles, characters and complex,
> independently of each other). I guess that would satisfy 90+% of the needs
> for a c( ) function in an Rcpp setting...
>
> Best regards
> Søren
>
>
> -----Original Message-----
> From: Kevin Ushey [mailto:[email protected]]
> Sent: 22. januar 2014 01:10
> To: Søren Højsgaard
> Cc: [email protected] ([email protected])
> Subject: Re: [Rcpp-devel] Rcpp version of c( )
>
> Hi Søren,
>
> I like the idea. Currently there is nothing like that in Rcpp. It could be
> made more flexible if we:
>
> 1. Accept a generic set of vectors that could be appropriately casted as
> needed, 2. Cast these vectors to the appropriate type if necessary, 3. Fill
> an output vector with the elements of these vectors.
>
> Of course, we would like to be able to accept primitives as well so that e.g.
> c(x, 1, y) works so something a bit more designed would be nice.
>
> There should be a flexible way to implement this using variadic templates, so
> that e.g.
>
> template <typename T, typename... Args>
> T c( Args... args ) {
> ...do the concatenation...
> }
>
> Of course, this requires C++11 so it might not fit until R 3.1.0 is out with
> its C++11 support, but it could be a natural fit in Rcpp11.
> Adding the generic version in Rcpp would require code bloat unfortunately.
>
> -Kevin
>
> On Tue, Jan 21, 2014 at 2:02 PM, Søren Højsgaard <[email protected]> wrote:
>> Dear all,
>>
>> I have made the following primitive "concatenate" function, because I
>> couldn't find one in Rcpp:
>>
>> template <const int RTYPE>
>> Vector<RTYPE> do_conc_(Vector<RTYPE> x, Vector<RTYPE> y){
>> int nx=x.size(), n=x.size()+y.size(),i,j;
>> Vector<RTYPE> out=no_init(n);
>> for (i=0; i<nx; ++i){ out[ i ] = x[ i ];}
>> for (j=i, i=0; j<n; ++j, ++i){ out[ j ] = y[i] ;}
>> return out;
>> }
>>
>> // [[Rcpp::export]]
>> SEXP conc( SEXP& XX_, SEXP& YY_){
>> int type = TYPEOF(XX_) ;
>> switch( type ){
>> case INTSXP : return do_conc_<INTSXP> ( XX_, YY_ ) ;
>> case REALSXP : return do_conc_<REALSXP>( XX_, YY_ ) ;
>> case STRSXP : return do_conc_<STRSXP> ( XX_, YY_ ) ;
>> case VECSXP : return do_conc_<VECSXP> ( XX_, YY_ ) ;
>> }
>> return R_NilValue ;
>> }
>>
>> As you can see it assumes that the two inputs XX_ and YY_ are of the same
>> type, and it fails to copy names to the output. If I have missed any such
>> functionality in Rcpp then I would be happy to know...
>>
>> Cheers
>> Søren
>>
>>
>>
>> _______________________________________________
>> Rcpp-devel mailing list
>> [email protected]
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-deve
>> l
> _______________________________________________
> Rcpp-devel mailing list
> [email protected]
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel