[Rcpp-commits] r376 - in pkg: inst src src/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jan 15 10:00:56 CET 2010


Author: romain
Date: 2010-01-15 10:00:56 +0100 (Fri, 15 Jan 2010)
New Revision: 376

Modified:
   pkg/inst/ChangeLog
   pkg/src/ComplexVector.cpp
   pkg/src/IntegerVector.cpp
   pkg/src/LogicalVector.cpp
   pkg/src/NumericVector.cpp
   pkg/src/RawVector.cpp
   pkg/src/Rcpp/ComplexVector.h
   pkg/src/Rcpp/IntegerVector.h
   pkg/src/Rcpp/LogicalVector.h
   pkg/src/Rcpp/NumericVector.h
   pkg/src/Rcpp/RawVector.h
Log:
same optimization for integer, raw, logical and complex vectors

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-15 08:43:15 UTC (rev 375)
+++ pkg/inst/ChangeLog	2010-01-15 09:00:56 UTC (rev 376)
@@ -4,6 +4,16 @@
 	improve performance of operator[]
 	* src/NumericVector.cpp: idem
 
+	* src/Rcpp/IntegerVector.h:
+	* src/Rcpp/RawVector.h:
+	* src/Rcpp/ComplexVector.h:
+	* src/Rcpp/LogicalVector.h: idem
+
+	* src/IntegerVector.cpp:
+	* src/RawVector.cpp:
+	* src/ComplexVector.cpp:
+	* src/LogicalVector.cpp: idem
+
 2010-01-13  Romain Francois <francoisromain at free.fr>
 
 	* R/cpp.package.skeleton.R: new function cpp.package.skeleton

Modified: pkg/src/ComplexVector.cpp
===================================================================
--- pkg/src/ComplexVector.cpp	2010-01-15 08:43:15 UTC (rev 375)
+++ pkg/src/ComplexVector.cpp	2010-01-15 09:00:56 UTC (rev 376)
@@ -26,31 +26,35 @@
 
 namespace Rcpp{
 	
-	ComplexVector::ComplexVector(SEXP x) throw(not_compatible) : VectorBase() {
+	ComplexVector::ComplexVector(SEXP x) throw(not_compatible) : VectorBase(), start(0) {
 		switch( TYPEOF( x ) ){
 			case CPLXSXP:
 				setSEXP( x ) ;
+				update() ;
 				break ;
 			case REALSXP:
 			case LGLSXP:
 			case RAWSXP:
 			case INTSXP:
 				setSEXP( Rf_coerceVector( x, CPLXSXP) ) ;
+				update() ;
 				break ;
 			default:
 				throw not_compatible( "cannot convert to complex vector" ) ;
 		}
 	}
 	
-	ComplexVector::ComplexVector(int size) : VectorBase() {
+	ComplexVector::ComplexVector(int size) : VectorBase(), start(0) {
 		setSEXP( Rf_allocVector(CPLXSXP, size) ) ;
+		update() ;
 	}
 
 #ifdef HAS_INIT_LISTS	
-ComplexVector::ComplexVector( std::initializer_list<Rcomplex> list ) : VectorBase() {
+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

Modified: pkg/src/IntegerVector.cpp
===================================================================
--- pkg/src/IntegerVector.cpp	2010-01-15 08:43:15 UTC (rev 375)
+++ pkg/src/IntegerVector.cpp	2010-01-15 09:00:56 UTC (rev 376)
@@ -26,36 +26,41 @@
 
 namespace Rcpp{
 	
-	IntegerVector::IntegerVector(SEXP x) throw(not_compatible) : VectorBase() {
+	IntegerVector::IntegerVector(SEXP x) throw(not_compatible) : VectorBase(), start(0) {
 		switch( TYPEOF( x ) ){
 			case INTSXP:
 				setSEXP( x ) ;
+				update();
 				break ;
 			case REALSXP:
 			case LGLSXP:
 			case RAWSXP:
 				setSEXP( Rf_coerceVector( x, INTSXP) ) ;
+				update() ;
 				break ;
 			default:
 				throw not_compatible( "cannot convert to intrger vector" ) ;
 		}
 	}
 	
-	IntegerVector::IntegerVector(int size) : VectorBase() {
+	IntegerVector::IntegerVector(int size) : VectorBase(), start(0) {
 		setSEXP( Rf_allocVector(INTSXP, size) ) ;
+		update() ;
 	}
 
 #ifdef HAS_INIT_LISTS	
-	IntegerVector::IntegerVector( std::initializer_list<int> list ) {
+	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 ) {
+	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

Modified: pkg/src/LogicalVector.cpp
===================================================================
--- pkg/src/LogicalVector.cpp	2010-01-15 08:43:15 UTC (rev 375)
+++ pkg/src/LogicalVector.cpp	2010-01-15 09:00:56 UTC (rev 376)
@@ -30,11 +30,13 @@
 		switch( TYPEOF( x ) ){
 			case LGLSXP:
 				setSEXP( x ) ;
+				update() ;
 				break ;
 			case RAWSXP:
 			case INTSXP:
 			case REALSXP:
 				setSEXP( Rf_coerceVector( x, LGLSXP) ) ;
+				update();
 				break ;
 			default:
 				throw not_compatible( "cannot convert to intrger vector" ) ;
@@ -43,6 +45,7 @@
 	
 	LogicalVector::LogicalVector(int size) : VectorBase() {
 		setSEXP( Rf_allocVector(LGLSXP, size) ) ;
+		update() ;
 	}
 
 #ifdef HAS_INIT_LISTS
@@ -50,18 +53,21 @@
 		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

Modified: pkg/src/NumericVector.cpp
===================================================================
--- pkg/src/NumericVector.cpp	2010-01-15 08:43:15 UTC (rev 375)
+++ pkg/src/NumericVector.cpp	2010-01-15 09:00:56 UTC (rev 376)
@@ -44,7 +44,7 @@
 	
 	NumericVector::NumericVector(int size) : VectorBase(), start(0) {
 		setSEXP( Rf_allocVector(REALSXP, size) ) ;
-		start = REAL(m_sexp) ;
+		update() ;
 	}
 
 #ifdef HAS_INIT_LISTS	

Modified: pkg/src/RawVector.cpp
===================================================================
--- pkg/src/RawVector.cpp	2010-01-15 08:43:15 UTC (rev 375)
+++ pkg/src/RawVector.cpp	2010-01-15 09:00:56 UTC (rev 376)
@@ -26,38 +26,41 @@
 
 namespace Rcpp{
 	
-	RawVector::RawVector(SEXP x) throw(not_compatible) : VectorBase() {
+	RawVector::RawVector(SEXP x) throw(not_compatible) : VectorBase(), start(0) {
 		switch( TYPEOF( x ) ){
 			case RAWSXP:
 				setSEXP( x ) ;
+				update() ;
 				break ;
 			case INTSXP:
 			case REALSXP:
 			case LGLSXP:
 				setSEXP( Rf_coerceVector( x, RAWSXP) ) ;
+				update() ;
 				break ;
 			default:
 				throw not_compatible( "cannot convert to intrger vector" ) ;
 		}
 	}
 	
-	RawVector::RawVector(int size) : VectorBase() {
+	RawVector::RawVector(int size) : VectorBase(), start(0) {
 		setSEXP( Rf_allocVector(RAWSXP, size) ) ;
+		update() ;
 	}
 
 #ifdef HAS_INIT_LISTS
-	RawVector::RawVector( std::initializer_list<int> list ) : VectorBase() {
+	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() {
-		/* FIXME: we need to take care of coercion, so 
-		transform is probably better */
+	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

Modified: pkg/src/Rcpp/ComplexVector.h
===================================================================
--- pkg/src/Rcpp/ComplexVector.h	2010-01-15 08:43:15 UTC (rev 375)
+++ pkg/src/Rcpp/ComplexVector.h	2010-01-15 09:00:56 UTC (rev 376)
@@ -43,12 +43,15 @@
 	ComplexVector( std::initializer_list<Rcomplex> list ) ;
 #endif
 	
-	inline Rcomplex& operator[]( int i ) const { return COMPLEX(m_sexp)[i] ; } 
-	inline Rcomplex* begin() const { return COMPLEX(m_sexp) ; } 
-	inline Rcomplex* end() const { return COMPLEX(m_sexp) + LENGTH(m_sexp) ; }
+	inline Rcomplex& operator[]( int i ) const { return start[i] ; } 
+	inline Rcomplex* begin() const { return start ; } 
+	inline Rcomplex* end() const { return start + LENGTH(m_sexp) ; }
 	
 	typedef Rcomplex* iterator ;
-	
+
+private:
+	Rcomplex* start ;
+	inline void update(){ start = COMPLEX(m_sexp);}
 } ;
 
 } // namespace

Modified: pkg/src/Rcpp/IntegerVector.h
===================================================================
--- pkg/src/Rcpp/IntegerVector.h	2010-01-15 08:43:15 UTC (rev 375)
+++ pkg/src/Rcpp/IntegerVector.h	2010-01-15 09:00:56 UTC (rev 376)
@@ -44,12 +44,15 @@
 	IntegerVector( std::initializer_list<double> list ) ;
 #endif
 
-	inline int& operator[]( int i ) const{ return INTEGER(m_sexp)[i] ; }
-	inline int* begin() const { return INTEGER(m_sexp) ; }
-	inline int* end() const { return INTEGER(m_sexp) + LENGTH(m_sexp) ; }
+	inline int& operator[]( int i ) const{ return start[i] ; }
+	inline int* begin() const { return start ; }
+	inline int* end() const { return start + LENGTH(m_sexp) ; }
 	
 	typedef int* iterator ;
-	
+
+private:
+	int* start ;
+	inline void update(){ start = INTEGER(m_sexp) ;}
 } ;
 
 } // namespace

Modified: pkg/src/Rcpp/LogicalVector.h
===================================================================
--- pkg/src/Rcpp/LogicalVector.h	2010-01-15 08:43:15 UTC (rev 375)
+++ pkg/src/Rcpp/LogicalVector.h	2010-01-15 09:00:56 UTC (rev 376)
@@ -31,26 +31,29 @@
 #include <algorithm>
 #endif
 
-namespace Rcpp{ 
+namespace Rcpp{
 
 class LogicalVector : public VectorBase {     
 public:
 
 	LogicalVector(SEXP x) throw(not_compatible);
 	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 ) ;
 #endif
-	
+
 	typedef int* iterator ;
-	
-	inline int& operator[]( int i ) const { return LOGICAL(m_sexp)[i] ;}
-	inline int* begin() const { return LOGICAL(m_sexp) ; }
-	inline int* end() const { return LOGICAL(m_sexp) + LENGTH(m_sexp); }
-	
+
+	inline int& operator[]( int i ) const { return start[i] ;}
+	inline int* begin() const { return start ; }
+	inline int* end() const { return start + LENGTH(m_sexp); }
+
+private:
+	int* start ;
+	inline void update(){ start=LOGICAL(m_sexp); }
 } ;
 
 } // namespace

Modified: pkg/src/Rcpp/NumericVector.h
===================================================================
--- pkg/src/Rcpp/NumericVector.h	2010-01-15 08:43:15 UTC (rev 375)
+++ pkg/src/Rcpp/NumericVector.h	2010-01-15 09:00:56 UTC (rev 376)
@@ -44,11 +44,6 @@
 	NumericVector( std::initializer_list<double> list ) ;
 #endif
 
-	// inline double& operator[]( const int& i ) { return REAL(m_sexp)[i]; }
-	// inline const double& operator[]( const int& i ) const { return REAL(m_sexp)[i]; }
-	// inline double* begin() const { return REAL(m_sexp) ; } 
-	// inline double* end() const   { return REAL(m_sexp)+LENGTH(m_sexp);}
-	
 	inline double& operator[]( const int& i ) { return start[i] ; }
 	inline const double& operator[]( const int& i ) const { return start[i]; }
 	inline double* begin() const { return start ; } 

Modified: pkg/src/Rcpp/RawVector.h
===================================================================
--- pkg/src/Rcpp/RawVector.h	2010-01-15 08:43:15 UTC (rev 375)
+++ pkg/src/Rcpp/RawVector.h	2010-01-15 09:00:56 UTC (rev 376)
@@ -44,12 +44,15 @@
 	RawVector( std::initializer_list<int> list ) ;
 #endif
 	
-	inline Rbyte& operator[]( int i ) const { return RAW(m_sexp)[i] ; }
-	inline Rbyte* begin() const { return RAW(m_sexp) ; }
-	inline Rbyte* end() const { return RAW(m_sexp) + LENGTH(m_sexp) ; }
+	inline Rbyte& operator[]( int i ) const { return start[i] ; }
+	inline Rbyte* begin() const { return start ; }
+	inline Rbyte* end() const { return start + LENGTH(m_sexp) ; }
 	
 	typedef Rbyte* iterator ;
-	
+
+private:
+	Rbyte* start ;
+	inline void update(){ start=RAW(m_sexp); }
 } ;
 
 } // namespace



More information about the Rcpp-commits mailing list