[Rcpp-devel] vector<int>

Romain François francoisromain at free.fr
Mon Dec 21 16:02:44 CET 2009


On 12/21/2009 03:39 PM, Dirk Eddelbuettel wrote:
>
> 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 :)

Yes, I know. Reading the books, I was kind of thinking about doing some 
sort of "smart external pointer".

SEXP fun( SEXP x){
	external_ptr< vector<int> > p = x ;
	p->size() ; /* ... */
}

but I'm not sure how to do this just yet, or if it is even possible.

> | 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
>

I was kind of asking myself (and you) the same question. As it is now, 
it could very well be a self contained package.

I really need S4 dispatch and formal inheritance for this, for example 
the CPP function as it is needs to be able to find out if "vector<int>" 
is a name of a class which extends "C++Object" ...

I guess I'll start it off as a separate beast, and then if we find that 
we need both way interaction between the two package, we can merge.

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/HlX9 : new package : bibtex
|- http://tr.im/Gq7i : ohloh
`- http://tr.im/FtUu : new package : highlight





More information about the Rcpp-devel mailing list