[Rcpp-devel] vector<int>

Dirk Eddelbuettel edd at debian.org
Mon Dec 21 14:59:12 CET 2009


(resending on list, hasn't meant to drop the list --D. )

On 21 December 2009 at 10:15, Romain François wrote:
| Attaching a patch to current Rcpp to allow playing with vector<int> at 
| the R level.
| 
| Here is a transcript to give a feel of what this does:
| 
|  > require(Rcpp)
| Loading required package: Rcpp
| Loading required package: methods
| 
|  > x <- new(CPP("vector<int>"))
| 
|  > x$push_back(1)
| 
|  > x$size()
| [1] 1
| 
|  > x$push_back(1:10)
| 
|  > x$size()
| [1] 11
| 
|  > x$capacity()
| [1] 16
| 
|  > x$reserve(50)
| 
|  > x$capacity()
| [1] 50
| 
|  > x$max_size()
| [1] 1073741823
| 
|  > x$as.vector()
|   [1]  1  1  2  3  4  5  6  7  8  9 10
| 
| 
| x if of S4 class "vector<int>" which extends the class "C++Object". 
| "C++Object" holds an external pointer to the C++ object that is wrapped.
| 
| The "C++Object" defines the "$" method which looks at the "typeof" of 
| its arguments and cooks the name of the c symbol used.
| 
| For example, when calling x$push_back(1:10), $.C++Object creates this C 
| symbol name: "vector_int____push_back___integer"
| 
| Then it is a case of implementing these, for example :
| 
| SEXP vector_int____push_back___integer(SEXP x, SEXP p1){
| 		std::vector<int> *p = (std::vector<int>*)EXTPTR_PTR(R_do_slot( x, 
| Rf_install("pointer") ) ) ;
| 		for( int i=0; i<LENGTH(p1); i++){
| 			p->push_back( INTEGER(p1)[i] ) ;
| 		}
| 		return(R_NilValue) ;
| 	}
| 
| 
| I've not commited because it is a relatively unstable idea, so I was 
| hoping to get comments before.

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, ... and the S4 mechanism
neither hurts nor helps here. And Rcpp is a natural place to play with this.  

I need to wrap up a release for RInside later today (ie write a short blog
post) and maybe post once or twice to r-packages about Rcpp and RInside over
the next few days.

Dirk

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


More information about the Rcpp-devel mailing list