[Rcpp-commits] r414 - in pkg: inst/unitTests src src/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jan 20 10:29:49 CET 2010


Author: romain
Date: 2010-01-20 10:29:48 +0100 (Wed, 20 Jan 2010)
New Revision: 414

Modified:
   pkg/inst/unitTests/runit.Function.R
   pkg/inst/unitTests/runit.Pairlist.R
   pkg/src/CharacterVector.cpp
   pkg/src/ComplexVector.cpp
   pkg/src/ExpressionVector.cpp
   pkg/src/GenericVector.cpp
   pkg/src/IntegerVector.cpp
   pkg/src/LogicalVector.cpp
   pkg/src/NumericVector.cpp
   pkg/src/RawVector.cpp
   pkg/src/Rcpp/CharacterVector.h
   pkg/src/Rcpp/ComplexVector.h
   pkg/src/Rcpp/ExpressionVector.h
   pkg/src/Rcpp/GenericVector.h
   pkg/src/Rcpp/IntegerVector.h
   pkg/src/Rcpp/LogicalVector.h
   pkg/src/Rcpp/NumericVector.h
   pkg/src/Rcpp/RawVector.h
Log:
moved all uses of #ifdef HAS_INIT_LISTS to headers

Modified: pkg/inst/unitTests/runit.Function.R
===================================================================
--- pkg/inst/unitTests/runit.Function.R	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/inst/unitTests/runit.Function.R	2010-01-20 09:29:48 UTC (rev 414)
@@ -37,7 +37,7 @@
 }
 
 test.Function.variadic <- function(){
-	if( Rcpp:::canUseCXX0X() ){
+	if( Rcpp:::capabilities()[["variadic templates"]] ){
 		funx <- cfunction(signature(x="function", y = "ANY"), '
 		Function sort(x) ;
 		return sort( y, Named("decreasing", true) ) ;

Modified: pkg/inst/unitTests/runit.Pairlist.R
===================================================================
--- pkg/inst/unitTests/runit.Pairlist.R	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/inst/unitTests/runit.Pairlist.R	2010-01-20 09:29:48 UTC (rev 414)
@@ -37,7 +37,7 @@
 }
 
 test.Pairlist.variadic <- function(){
-	if( Rcpp:::canUseCXX0X() ){
+	if( Rcpp:::capabilities()[["variadic templates"]] ){
 		funx <- cfunction(signature(), '
 		return Pairlist( "rnorm", 10, 0.0, 2.0 ) ;
 		', Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;",

Modified: pkg/src/CharacterVector.cpp
===================================================================
--- pkg/src/CharacterVector.cpp	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/CharacterVector.cpp	2010-01-20 09:29:48 UTC (rev 414)
@@ -51,28 +51,9 @@
 	}
 	
 	CharacterVector::CharacterVector( const std::vector<std::string>& x): VectorBase() {
-		SEXP y = PROTECT( Rf_allocVector( STRSXP, x.size() ) );
-		int n = x.size() ;
-		std::vector<std::string>::const_iterator iter = x.begin() ;
-		for( int i=0; i<n; i++, iter++){
-			SET_STRING_ELT( y, i, Rf_mkChar(iter->c_str()) ) ;
-		}
-		setSEXP(y) ;
-		UNPROTECT(1) ;
+		fill( x.begin(), x.size() ) ;
 	}
 	
-#ifdef HAS_INIT_LISTS
-CharacterVector::CharacterVector( std::initializer_list<std::string> list ) : VectorBase() {
-		SEXP x = PROTECT( Rf_allocVector( STRSXP, list.size() ) ) ;
-		const std::string *p = list.begin() ;
-		int n = list.size() ;
-		for( int i=0; i<n ; i++, p++){
-			SET_STRING_ELT( x, i, Rf_mkChar(p->c_str()) ) ;
-		}
-		setSEXP( x ) ;
-		UNPROTECT( 1 ); /* x */
-	}
-#endif
 
 /* proxy stuff */
 

Modified: pkg/src/ComplexVector.cpp
===================================================================
--- pkg/src/ComplexVector.cpp	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/ComplexVector.cpp	2010-01-20 09:29:48 UTC (rev 414)
@@ -49,14 +49,4 @@
 		update() ;
 	}
 
-#ifdef HAS_INIT_LISTS	
-ComplexVector::ComplexVector( std::initializer_list<Rcomplex> list ) : VectorBase(), start(0) {
-		SEXP x = PROTECT( Rf_allocVector( CPLXSXP, list.size() ) ) ;
-		std::copy( list.begin(), list.end(), COMPLEX(x) ); 
-		setSEXP(x) ;
-		update() ;
-		UNPROTECT( 1 ); /* x */
-	}
-#endif
-
 } // namespace 

Modified: pkg/src/ExpressionVector.cpp
===================================================================
--- pkg/src/ExpressionVector.cpp	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/ExpressionVector.cpp	2010-01-20 09:29:48 UTC (rev 414)
@@ -68,18 +68,6 @@
 			throw parse_error() ;
 		}
 	}
-	
-#ifdef HAS_INIT_LISTS
-	ExpressionVector::ExpressionVector( std::initializer_list<RObject> list ) : VectorBase() {
-		SEXP x = PROTECT( Rf_allocVector( EXPRSXP, list.size() ) ) ;
-		const RObject* p = list.begin() ;
-		for( size_t i=0; i<list.size() ; i++, p++){
-			SET_VECTOR_ELT( x, i, p->asSexp() ) ;
-		}
-		setSEXP( x ) ;
-		UNPROTECT( 1 ); /* x */
-	}
-#endif
 
 ExpressionVector::Proxy::Proxy(ExpressionVector& v, int i) :
 	parent(v), index(i){}

Modified: pkg/src/GenericVector.cpp
===================================================================
--- pkg/src/GenericVector.cpp	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/GenericVector.cpp	2010-01-20 09:29:48 UTC (rev 414)
@@ -50,18 +50,6 @@
 		setSEXP( Rf_allocVector(VECSXP, size) ) ;
 	}
 
-#ifdef HAS_INIT_LISTS
-GenericVector::GenericVector( std::initializer_list<RObject> list ) : VectorBase() {
-		SEXP x = PROTECT( Rf_allocVector( VECSXP, list.size() ) ) ;
-		const RObject* p = list.begin() ;
-		for( size_t i=0; i<list.size() ; i++, p++){
-			SET_VECTOR_ELT( x, i, p->asSexp() ) ;
-		}
-		setSEXP( x ) ;
-		UNPROTECT( 1 ); /* x */
-	}
-#endif
-
 /* proxy stuff */
 
 GenericVector::Proxy::Proxy(GenericVector& v, int i) :

Modified: pkg/src/IntegerVector.cpp
===================================================================
--- pkg/src/IntegerVector.cpp	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/IntegerVector.cpp	2010-01-20 09:29:48 UTC (rev 414)
@@ -22,7 +22,6 @@
 #include <RcppCommon.h>
 #include <Rcpp/RObject.h>
 #include <Rcpp/IntegerVector.h>
-#include <algorithm>
 
 namespace Rcpp{
 	
@@ -48,21 +47,4 @@
 		update() ;
 	}
 
-#ifdef HAS_INIT_LISTS	
-	IntegerVector::IntegerVector( std::initializer_list<int> list ) : VectorBase(), start(0) {
-		SEXP x = PROTECT( Rf_allocVector( INTSXP, list.size() ) ) ;
-		std::copy( list.begin(), list.end(), INTEGER(x) ); 
-		setSEXP(x) ;
-		update() ;
-		UNPROTECT( 1 ); /* x */
-	}
-	IntegerVector::IntegerVector( std::initializer_list<double> list ) : VectorBase(), start(0) {
-		SEXP x = PROTECT( Rf_allocVector( INTSXP, list.size() ) ) ;
-		std::copy( list.begin(), list.end(), INTEGER(x) ); 
-		setSEXP(x) ;
-		update() ;
-		UNPROTECT( 1 ); /* x */
-	}
-#endif
-
 } // namespace 

Modified: pkg/src/LogicalVector.cpp
===================================================================
--- pkg/src/LogicalVector.cpp	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/LogicalVector.cpp	2010-01-20 09:29:48 UTC (rev 414)
@@ -48,28 +48,4 @@
 		update() ;
 	}
 
-#ifdef HAS_INIT_LISTS
-	LogicalVector::LogicalVector( std::initializer_list<int> list ) : VectorBase(){
-		SEXP x = PROTECT( Rf_allocVector( INTSXP, list.size() ) ) ;
-		std::copy( list.begin(), list.end(), INTEGER(x) ); 
-		setSEXP( Rf_coerceVector( x, LGLSXP ) ) ;
-		update() ;
-		UNPROTECT( 1 ); /* x */
-	}
-	LogicalVector::LogicalVector( std::initializer_list<Rboolean> list ): VectorBase() {
-		SEXP x = PROTECT( Rf_allocVector( LGLSXP, list.size() ) ) ;
-		std::copy( list.begin(), list.end(), LOGICAL(x) ); 
-		setSEXP(x) ;
-		update() ;
-		UNPROTECT( 1 ); /* x */
-	}
-	LogicalVector::LogicalVector( std::initializer_list<bool> list ) : VectorBase(){
-		SEXP x = PROTECT( Rf_allocVector( LGLSXP, list.size() ) ) ;
-		std::copy( list.begin(), list.end(), LOGICAL(x) ); 
-		setSEXP(x) ;
-		update() ;
-		UNPROTECT( 1 ); /* x */
-	}
-#endif
-
 } // namespace 

Modified: pkg/src/NumericVector.cpp
===================================================================
--- pkg/src/NumericVector.cpp	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/NumericVector.cpp	2010-01-20 09:29:48 UTC (rev 414)
@@ -47,21 +47,4 @@
 		update() ;
 	}
 
-#ifdef HAS_INIT_LISTS	
-NumericVector::NumericVector( std::initializer_list<int> list ) : VectorBase(), start(0) {
-		SEXP x = PROTECT( Rf_allocVector( REALSXP, list.size() ) ) ;
-		std::copy( list.begin(), list.end(), REAL(x) ); 
-		setSEXP(x);
-		update() ;
-		UNPROTECT( 1 ); /* x */
-	}
-	NumericVector::NumericVector( std::initializer_list<double> list ) : VectorBase(), start(0) {
-		SEXP x = PROTECT( Rf_allocVector( REALSXP, list.size() ) ) ;
-		std::copy( list.begin(), list.end(), REAL(x) ); 
-		setSEXP(x);
-		update() ;
-		UNPROTECT( 1 ); /* x */
-	}
-#endif
-
 } // namespace 

Modified: pkg/src/RawVector.cpp
===================================================================
--- pkg/src/RawVector.cpp	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/RawVector.cpp	2010-01-20 09:29:48 UTC (rev 414)
@@ -48,21 +48,4 @@
 		update() ;
 	}
 
-#ifdef HAS_INIT_LISTS
-	RawVector::RawVector( std::initializer_list<int> list ) : VectorBase(), start(0) {
-		SEXP x = PROTECT( Rf_allocVector( RAWSXP, list.size() ) ) ;
-		std::copy( list.begin(), list.end(), RAW(x) ); 
-		setSEXP(x) ;
-		update() ;
-		UNPROTECT( 1 ); /* x */
-	}
-	RawVector::RawVector( std::initializer_list<Rbyte> list ) : VectorBase(), start(0) {
-		SEXP x = PROTECT( Rf_allocVector( RAWSXP, list.size() ) ) ;
-		std::copy( list.begin(), list.end(), RAW(x) ); 
-		setSEXP(x) ;
-		update() ;
-		UNPROTECT( 1 ); /* x */
-	}
-#endif
-
 } // namespace 

Modified: pkg/src/Rcpp/CharacterVector.h
===================================================================
--- pkg/src/Rcpp/CharacterVector.h	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/Rcpp/CharacterVector.h	2010-01-20 09:29:48 UTC (rev 414)
@@ -61,9 +61,11 @@
 	CharacterVector(int size) ;
 	CharacterVector( const std::string& x );
 	CharacterVector( const std::vector<std::string>& x );
-
+	
 #ifdef HAS_INIT_LISTS
-	CharacterVector( std::initializer_list<std::string> list ) ;
+	CharacterVector( std::initializer_list<std::string> list ) : VectorBase() {
+		fill( list.begin(), list.size() ) ;
+	}
 #endif
 
 	const StringProxy operator[]( int i ) const throw(index_out_of_bounds);
@@ -71,6 +73,16 @@
 
 	friend class StringProxy; 
 
+private:
+	template <typename InputIterator>
+	void fill(InputIterator first, size_t size ){
+		SEXP x = PROTECT( Rf_allocVector( STRSXP, size) ) ;
+		for( size_t i=0; i<size; ++i, ++first ){
+			SET_STRING_ELT( x, i, Rf_mkChar(first->c_str()) ) ;
+		}
+		setSEXP( x );
+		UNPROTECT(1) ;
+	}
 } ;
 
 typedef CharacterVector StringVector ;

Modified: pkg/src/Rcpp/ComplexVector.h
===================================================================
--- pkg/src/Rcpp/ComplexVector.h	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/Rcpp/ComplexVector.h	2010-01-20 09:29:48 UTC (rev 414)
@@ -40,7 +40,9 @@
 	ComplexVector(int size) ;
 	
 #ifdef HAS_INIT_LISTS	
-	ComplexVector( std::initializer_list<Rcomplex> list ) ;
+	ComplexVector( std::initializer_list<Rcomplex> list ) : VectorBase(){
+		fill( list.begin(), list.end() ) ;
+	};
 #endif
 	
 	inline Rcomplex& operator[]( int i ) const { return start[i] ; } 
@@ -52,6 +54,16 @@
 private:
 	Rcomplex* start ;
 	inline void update(){ start = COMPLEX(m_sexp);}
+	
+	template <typename InputIterator>
+	void fill( InputIterator first, InputIterator last){
+		size_t size = std::distance(first, last) ;
+		SEXP x = PROTECT( Rf_allocVector( CPLXSXP, size ) ) ;
+		std::copy( first, last, COMPLEX(x) ) ;
+		setSEXP(x) ;
+		update() ;
+		UNPROTECT( 1 ); /* x */
+	}
 } ;
 
 } // namespace

Modified: pkg/src/Rcpp/ExpressionVector.h
===================================================================
--- pkg/src/Rcpp/ExpressionVector.h	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/Rcpp/ExpressionVector.h	2010-01-20 09:29:48 UTC (rev 414)
@@ -75,7 +75,9 @@
 	SEXP eval(const Environment& env) throw(Evaluator::eval_error);
 	
 #ifdef HAS_INIT_LISTS	
-	ExpressionVector( std::initializer_list<RObject> list ) ;
+	ExpressionVector( std::initializer_list<SEXP> list ) : VectorBase(){
+		  fill( list.begin(), list.end() ) ;
+	}
 #endif
 
 	const Proxy operator[]( int i ) const throw(index_out_of_bounds);
@@ -83,6 +85,18 @@
 
 	friend class Proxy; 
 
+private:
+	template <typename InputIterator>
+	void fill( InputIterator first, InputIterator last){
+		size_t size = std::distance( first, last );
+		SEXP x = PROTECT( Rf_allocVector( EXPRSXP, size ) ) ;
+		for( size_t i=0; i<size ; i++, ++first){
+			SET_VECTOR_ELT( x, i, *first ) ;
+		}
+		setSEXP( x ) ;
+		UNPROTECT( 1 ); /* x */
+	}
+
 } ;
 
 } // namespace

Modified: pkg/src/Rcpp/GenericVector.h
===================================================================
--- pkg/src/Rcpp/GenericVector.h	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/Rcpp/GenericVector.h	2010-01-20 09:29:48 UTC (rev 414)
@@ -66,13 +66,26 @@
 	GenericVector( int size) ;
 	
 #ifdef HAS_INIT_LISTS	
-	GenericVector( std::initializer_list<RObject> list ) ;
+	GenericVector( std::initializer_list<RObject> list ) : VectorBase(){
+		fill( list.begin(), list.end() ) ;
+	};
 #endif
 	
 	const Proxy operator[]( int i ) const throw(index_out_of_bounds);
 	Proxy operator[]( int i ) throw(index_out_of_bounds) ;
 	
 	friend class Proxy; 
+
+	template <typename InputIterator>
+	void fill( InputIterator first, InputIterator last){
+		size_t size = std::distance( first, last );
+		SEXP x = PROTECT( Rf_allocVector( VECSXP, size ) ) ;
+		for( size_t i=0; i<size ; i++, ++first){
+			SET_VECTOR_ELT( x, i, *first ) ;
+		}
+		setSEXP( x ) ;
+		UNPROTECT( 1 ); /* x */
+	}
 	
 } ;
 

Modified: pkg/src/Rcpp/IntegerVector.h
===================================================================
--- pkg/src/Rcpp/IntegerVector.h	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/Rcpp/IntegerVector.h	2010-01-20 09:29:48 UTC (rev 414)
@@ -37,11 +37,15 @@
 public:
 
 	IntegerVector(SEXP x) throw(not_compatible);
-	IntegerVector( int size) ;
+	IntegerVector(int size) ;
 	
 #ifdef HAS_INIT_LISTS	
-	IntegerVector( std::initializer_list<int> list ) ;
-	IntegerVector( std::initializer_list<double> list ) ;
+	IntegerVector( std::initializer_list<int> list ): VectorBase(), start(0) { 
+		simple_fill( list.begin(), list.end() );
+	}
+	IntegerVector( std::initializer_list<double> list ) :VectorBase(), start(0) { 
+		coerce_and_fill( list.begin(), list.end() );
+	}
 #endif
 
 	inline int& operator[]( int i ) const{ return start[i] ; }
@@ -53,6 +57,31 @@
 private:
 	int* start ;
 	inline void update(){ start = INTEGER(m_sexp) ;}
+	
+	// simple is when there is no need for coercion
+	template <typename InputIterator>
+	void simple_fill( InputIterator first, InputIterator last){
+		size_t size = std::distance( first, last) ;
+		SEXP x = PROTECT( Rf_allocVector( INTSXP, size ) );
+		std::copy( first, last, INTEGER(x) ) ;
+		setSEXP( x ) ;
+		UNPROTECT(1) ;
+		update() ;
+	}
+	
+	// need to coerce to int
+	template <typename InputIterator>
+	void coerce_and_fill( InputIterator first, InputIterator last){
+		size_t size = std::distance( first, last) ;
+		SEXP x = PROTECT( Rf_allocVector( INTSXP, size ) );
+		// FIXME: actually coerce
+		// std::transform( first, last, INTEGER(x), coerce<int> ) ;
+		std::copy( first, last, INTEGER(x) ) ;
+		setSEXP( x ) ;
+		UNPROTECT(1) ;
+		update() ;
+	}
+
 } ;
 
 } // namespace

Modified: pkg/src/Rcpp/LogicalVector.h
===================================================================
--- pkg/src/Rcpp/LogicalVector.h	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/Rcpp/LogicalVector.h	2010-01-20 09:29:48 UTC (rev 414)
@@ -40,9 +40,15 @@
 	LogicalVector( int size) ;
 
 #ifdef HAS_INIT_LISTS	
-	LogicalVector( std::initializer_list<int> list ) ;
-	LogicalVector( std::initializer_list<Rboolean> list ) ;
-	LogicalVector( std::initializer_list<bool> list ) ;
+	LogicalVector( std::initializer_list<Rboolean> list ) : VectorBase(), start(0) {
+		simple_fill( list.begin(), list.end() ) ;
+	}
+	LogicalVector( std::initializer_list<int> list ) : VectorBase(), start(0) {
+		coerce_and_fill( list.begin(), list.end() ) ;
+	}
+	LogicalVector( std::initializer_list<bool> list ) : VectorBase(), start(0) {
+		coerce_and_fill( list.begin(), list.end() ) ;
+	}
 #endif
 
 	typedef int* iterator ;
@@ -54,6 +60,31 @@
 private:
 	int* start ;
 	inline void update(){ start=LOGICAL(m_sexp); }
+	
+	// called when there is no need for coercion
+	template <typename InputIterator>
+	void simple_fill( InputIterator first, InputIterator last){
+		size_t size = std::distance( first, last ) ;
+		SEXP x = PROTECT( Rf_allocVector( LGLSXP, size ) ) ;
+		std::copy( first, last, LOGICAL(x) ); 
+		setSEXP(x) ;
+		update() ;
+		UNPROTECT( 1 ); /* x */
+	}
+	
+	template <typename InputIterator>
+	void coerce_and_fill( InputIterator first, InputIterator last){
+		size_t size = std::distance( first, last ) ;
+		SEXP x = PROTECT( Rf_allocVector( LGLSXP, size ) ) ;
+		// FIXME : actually coerce
+		// std::transform( first, last, LOGICAL(x), coerce_to_logical ) ;
+		std::copy( first, last, LOGICAL(x) ); 
+		setSEXP(x) ;
+		update() ;
+		UNPROTECT( 1 ); /* x */
+	}
+	
+	
 } ;
 
 } // namespace

Modified: pkg/src/Rcpp/NumericVector.h
===================================================================
--- pkg/src/Rcpp/NumericVector.h	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/Rcpp/NumericVector.h	2010-01-20 09:29:48 UTC (rev 414)
@@ -40,8 +40,12 @@
 	NumericVector( int size) ;
 	
 #ifdef HAS_INIT_LISTS	
-	NumericVector( std::initializer_list<int> list ) ;
-	NumericVector( std::initializer_list<double> list ) ;
+	NumericVector( std::initializer_list<int> list ) : VectorBase(), start(0){
+		coerce_and_fill( list.begin(), list.end() ) ;
+	};
+	NumericVector( std::initializer_list<double> list ) : VectorBase(), start(0){
+		simple_fill( list.begin(), list.end() ) ;
+	}
 #endif
 
 	inline double& operator[]( const int& i ) { return start[i] ; }
@@ -54,6 +58,33 @@
 private:
 	double *start ;
 	inline void update(){ start = REAL(m_sexp);}
+	
+	// simple is when there is no need for coercion
+	// called only when the input container contains doubles
+	template <typename InputIterator>
+	void simple_fill( InputIterator first, InputIterator last){
+		size_t size = std::distance( first, last) ;
+		SEXP x = PROTECT( Rf_allocVector( REALSXP, size ) );
+		std::copy( first, last, REAL(x) ) ;
+		setSEXP( x ) ;
+		UNPROTECT(1) ;
+		update() ;
+	}
+	
+	// need to coerce to double
+	template <typename InputIterator>
+	void coerce_and_fill( InputIterator first, InputIterator last){
+		size_t size = std::distance( first, last) ;
+		SEXP x = PROTECT( Rf_allocVector( REALSXP, size ) );
+		// FIXME: actually coerce
+		// std::transform( first, last, REAL(x), coerce_to_double ) ;
+		std::copy( first, last, REAL(x) ) ;
+		setSEXP( x ) ;
+		UNPROTECT(1) ;
+		update() ;
+	}
+	
+	
 } ;
 
 } // namespace

Modified: pkg/src/Rcpp/RawVector.h
===================================================================
--- pkg/src/Rcpp/RawVector.h	2010-01-20 07:51:16 UTC (rev 413)
+++ pkg/src/Rcpp/RawVector.h	2010-01-20 09:29:48 UTC (rev 414)
@@ -40,8 +40,12 @@
 	RawVector( int size) ;
 	
 #ifdef HAS_INIT_LISTS	
-	RawVector( std::initializer_list<Rbyte> list ) ;
-	RawVector( std::initializer_list<int> list ) ;
+	RawVector( std::initializer_list<Rbyte> list ) : VectorBase(), start(0) {
+		simple_fill( list.begin(), list.end() ) ;
+	}
+	RawVector( std::initializer_list<int> list ) : VectorBase(), start(0) {
+		coerce_and_fill( list.begin(), list.end() ) ;
+	}
 #endif
 	
 	inline Rbyte& operator[]( int i ) const { return start[i] ; }
@@ -53,6 +57,32 @@
 private:
 	Rbyte* start ;
 	inline void update(){ start=RAW(m_sexp); }
+	
+	// simple is when there is no need for coercion
+	// called only when the input container contains raw bytes
+	template <typename InputIterator>
+	void simple_fill( InputIterator first, InputIterator last){
+		size_t size = std::distance( first, last) ;
+		SEXP x = PROTECT( Rf_allocVector( RAWSXP, size ) );
+		std::copy( first, last, RAW(x) ) ;
+		setSEXP( x ) ;
+		UNPROTECT(1) ;
+		update() ;
+	}
+	
+	// need to coerce to raw bytes
+	template <typename InputIterator>
+	void coerce_and_fill( InputIterator first, InputIterator last){
+		size_t size = std::distance( first, last) ;
+		SEXP x = PROTECT( Rf_allocVector( RAWSXP, size ) );
+		// FIXME: actually coerce
+		// std::transform( first, last, RAW(x), coerce<RAWSXP> ) ;
+		std::copy( first, last, RAW(x) ) ;
+		setSEXP( x ) ;
+		UNPROTECT(1) ;
+		update() ;
+	}
+	
 } ;
 
 } // namespace



More information about the Rcpp-commits mailing list