[Rcpp-devel] exposing std::vector<double> with Rcpp modules
Romain Francois
romain at r-enthusiasts.com
Sat May 29 08:44:24 CEST 2010
Hello,
Yesterday I've tried to expose std::vector<double> to R with Rcpp
modules, and this led to some changes in the code.
One problem was that sometimes a class (and we do this in Rcpp
sometimes) defines a const and a non const version of the same method.
For example std::vector defines "back" twice :
reference back ( );
const_reference back ( ) const;
and so using .method( "back", &vec::back ) did not work because the
compiler was confused about which one to use.
I have resolved this by introducing const_method and nonconst_method
that work the same as method but will only expose the const version
(const_method) or the non const version (for nonconst_method).
I've updated the vignette to include documentation about this and an
example on how to expose std::vector<double>.
http://addictedtor.free.fr/misc/rcpp/Rcpp-modules.pdf
typedef std::vector<double> vec ;
void vec_assign( vec* obj, Rcpp::NumericVector data ){
obj->assign( data.begin(), data.end() ) ;
}
void vec_insert( vec* obj, int position, Rcpp::NumericVector data){
vec::iterator it = obj->begin() + position ;
obj->insert( it, data.begin(), data.end() ) ;
}
Rcpp::NumericVector vec_asR( vec* obj){
return Rcpp::wrap( *obj ) ;
}
RCPP_MODULE(yada){
using namespace Rcpp ;
class_<vec>( "vec")
.method( "size", &vec::size)
.method( "max_size", &vec::max_size)
.method( "resize", &vec::resize)
.method( "capacity", &vec::capacity)
.method( "empty", &vec::empty)
.method( "reserve", &vec::reserve)
.method( "push_back", &vec::push_back )
.method( "pop_back", &vec::pop_back )
.method( "clear", &vec::clear )
.const_method( "back", &vec::back )
.const_method( "front", &vec::front )
.const_method( "at", &vec::at )
.method( "assign", &vec_assign )
.method( "insert", &vec_insert )
.method( "as.vector", &vec_asR )
;
}
Romain
--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/9CQ66r : RMetrics 2010
|- http://bit.ly/cork4b : highlight 0.1-8
`- http://bit.ly/bklUXt : RcppArmadillo 0.2.1
More information about the Rcpp-devel
mailing list