[Rcpp-devel] vector<int>

Dirk Eddelbuettel edd at debian.org
Mon Dec 21 15:39:17 CET 2009


On 21 December 2009 at 15:12, Romain François wrote:
| On 12/21/2009 02:59 PM, Dirk Eddelbuettel wrote:
| > Interesting -- certainly very nice to have all the core parts of vector<int>
| > at the R level.  I have to think about how / where I would use that in my
| > workflow.  I never think much about the C++ object "per se" -- they are just
| > containers to call something, compute something, ...
| 
| Sure, you can then do things like this with the inline stuff:
| 
| 
| #!/usr/bin/r
| 
| suppressMessages(library(Rcpp))
| 
| x <- new( CPP("vector<int>") )
| x$push_back(sample(1:10))
| 
| foo <- '
|      std::vector<int> *p = (std::vector<int> 
| *)EXTPTR_PTR(R_do_slot(v,Rf_install("pointer"))) ;
|      sort( p->begin(), p->end() ) ;
|      return( R_NilValue ) ;
| '
| 
| funx <- cfunction(signature(v="vector<int>"), foo, Rcpp=TRUE, includes = 
| "#include <algorithm>")
| funx(x)
| x$as.vector()
| 
| 
| The hairy bit is probably the first line of the code :
| 
| std::vector<int> *p = (std::vector<int> 
| *)EXTPTR_PTR(R_do_slot(v,Rf_install("pointer"))) ;

Yikes :)   That doesn't exactly hide the C/C++ complexity from users :)

| but then we could tweak cfunction so that the generated code directly 
| gets the vector<int> pointer instead.

Hm. I tried to be as lightweight as possible in my changes to inline /
cfunction as I still hope Oleg will find the few minutes to patch / adapt it
into _his_ package.  But I have been waiting for a while already so not sure
if / when this happens.

| > and the S4 mechanism
| > neither hurts nor helps here.
| 
| You said you did not want S4 before, but then you opened pandora's box 
| with the inline experiment, which needs methods.

Right. Because I took it in 'as is'. I started chopping his methods bits out
but then reverted as that was silly.   

I don't use S4 anywhere in Rcpp and don't plan to right now. I am hoping that
the cfunction() code can go pack to inline at which point I'd remove methods
etc.  Unless I have reasons to use it.

| > And Rcpp is a natural place to play with this.
| 
| cool. Is that a go ?

Dunno. I am still not sure exactly how useful it is (for me, ie useful in the
narrow sense).

It is damn cool though and the technique needs to be exposed more.  Would
that make more sense in Rcpp or in a standalone 'CPP' or whatever you had
joked about?

Dirk

-- 
Three out of two people have difficulties with fractions.


More information about the Rcpp-devel mailing list