[Rcpp-devel] vector<int>

Romain François francoisromain at free.fr
Mon Dec 21 10:15:23 CET 2009


Hi there,

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.

Romain

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

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: vector_int_.diff
Url: http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20091221/5cc04d4c/attachment.txt 


More information about the Rcpp-devel mailing list