[Rcpp-commits] r1701 - in pkg/Rcpp/inst: examples/ConvolveBenchmarks include/Rcpp/sugar/operators include/Rcpp/vector unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 24 09:41:51 CEST 2010


Author: romain
Date: 2010-06-24 09:41:51 +0200 (Thu, 24 Jun 2010)
New Revision: 1701

Modified:
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve5_cpp.cpp
   pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h
   pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h
   pkg/Rcpp/inst/unitTests/runit.sugar.times.R
Log:
some adjustments to operator*

Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve5_cpp.cpp
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve5_cpp.cpp	2010-06-24 03:43:37 UTC (rev 1700)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve5_cpp.cpp	2010-06-24 07:41:51 UTC (rev 1701)
@@ -8,10 +8,8 @@
 RcppExport SEXP convolve5cpp(SEXP a, SEXP b) {
     NumericVector xa(a); int n_xa = xa.size() ;
     NumericVector xb(b); int n_xb = xb.size() ;
-    int nab = n_xa + n_xb - 1;
+    NumericVector xab(n_xa + n_xb - 1,0.0);
     
-    NumericVector xab(nab,0.0);
-    
     for(int i=0; i<n_xa; i++){
     	xab[ Range(i, i+n_xb-1) ] += xa[i] * xb ;
     }

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h	2010-06-24 03:43:37 UTC (rev 1700)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h	2010-06-24 07:41:51 UTC (rev 1701)
@@ -25,109 +25,197 @@
 namespace Rcpp{
 namespace sugar{
 
-	template <int RTYPE,bool LHS_NA, bool RHS_NA>
-	class times{
+	template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T >
+	class Times_Vector_Vector : public Rcpp::VectorBase<RTYPE, true , Times_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
 	public:
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		inline STORAGE apply( STORAGE lhs, STORAGE rhs) const {
-			return traits::is_na<RTYPE>(lhs) ? lhs : ( traits::is_na<RTYPE>(rhs) ? rhs : (lhs * rhs) ) ;
+		typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
+		typedef typename Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
+		
+		Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
+			lhs(lhs_), rhs(rhs_){}
+		
+		inline STORAGE operator[]( int i ) const {
+			STORAGE lhs_ = lhs[i] ;
+			if( traits::is_na<RTYPE>(lhs_) ) return lhs_ ;
+			STORAGE rhs_ = rhs[i] ;
+			return traits::is_na<RTYPE>(rhs_) ? rhs_ : (lhs_ * rhs_) ;
 		}
-	} ;
-	template <int RTYPE,bool RHS_NA>
-	class times<RTYPE,false,RHS_NA>{
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const LHS_TYPE& lhs ;
+		const RHS_TYPE& rhs ;
+	} ;	
+	
+	// specialization LHS_NA = false
+	template <int RTYPE, typename LHS_T, bool RHS_NA, typename RHS_T >
+	class Times_Vector_Vector<RTYPE,false,LHS_T,RHS_NA,RHS_T> : public Rcpp::VectorBase<RTYPE,true, Times_Vector_Vector<RTYPE,false,LHS_T,RHS_NA,RHS_T> > {
 	public:
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		inline STORAGE apply( STORAGE lhs, STORAGE rhs) const {
-			return traits::is_na<RTYPE>(rhs) ? rhs : (lhs * rhs);
+		typedef typename Rcpp::VectorBase<RTYPE,false,LHS_T> LHS_TYPE ;
+		typedef typename Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
+		
+		Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
+			lhs(lhs_), rhs(rhs_){}
+		
+		inline STORAGE operator[]( int i ) const {
+			STORAGE rhs_ = rhs[i] ;
+			if( traits::is_na<RTYPE>(rhs_) ) return rhs_ ;
+			return lhs[i] * rhs_  ;
 		}
-	} ;
-	template <int RTYPE,bool LHS_NA>
-	class times<RTYPE,LHS_NA,false>{
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const LHS_TYPE& lhs ;
+		const RHS_TYPE& rhs ;
+	} ;	
+
+	// specialization for RHS_NA = false 
+	template <int RTYPE, bool LHS_NA, typename LHS_T, typename RHS_T >
+	class Times_Vector_Vector<RTYPE,LHS_NA,LHS_T,false,RHS_T> : public Rcpp::VectorBase<RTYPE, true , Times_Vector_Vector<RTYPE,LHS_NA,LHS_T,false,RHS_T> > {
 	public:
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		inline STORAGE apply( STORAGE lhs, STORAGE rhs) const {
-			return traits::is_na<RTYPE>(lhs) ? lhs : (lhs * rhs);
+		typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
+		typedef typename Rcpp::VectorBase<RTYPE,false,RHS_T> RHS_TYPE ;
+		
+		Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
+			lhs(lhs_), rhs(rhs_){}
+		
+		inline STORAGE operator[]( int i ) const {
+			STORAGE lhs_ = lhs[i] ;
+			if( traits::is_na<RTYPE>(lhs_) ) return lhs_ ;
+			return lhs_ * rhs[i]  ;
 		}
-	} ;
-	template <int RTYPE>
-	class times<RTYPE,false,false>{
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const LHS_TYPE& lhs ;
+		const RHS_TYPE& rhs ;
+	} ;	
+
+
+	// specialization for RHS_NA = false  and LHS_NA = false 
+	template <int RTYPE, typename LHS_T, typename RHS_T >
+	class Times_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> : public Rcpp::VectorBase<RTYPE, false , Times_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> > {
 	public:
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		inline STORAGE apply( STORAGE lhs, STORAGE rhs) const {
-			return lhs * rhs;
+		typedef typename Rcpp::VectorBase<RTYPE,false,LHS_T> LHS_TYPE ;
+		typedef typename Rcpp::VectorBase<RTYPE,false,RHS_T> RHS_TYPE ;
+		
+		Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
+			lhs(lhs_), rhs(rhs_){}
+		
+		inline STORAGE operator[]( int i ) const {
+			return lhs[i] * rhs[i]  ;
 		}
-	} ;
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const LHS_TYPE& lhs ;
+		const RHS_TYPE& rhs ;
+	} ;	
 	
-
-	template <int RTYPE, bool _NA_, typename VEC_TYPE>
-	class Times_Vector_Primitive : public Rcpp::VectorBase<RTYPE,true, Times_Vector_Primitive<RTYPE,_NA_,VEC_TYPE> > {
+	
+	
+	template <int RTYPE, bool NA, typename T>
+	class Times_Vector_Primitive : public Rcpp::VectorBase<RTYPE,true, Times_Vector_Primitive<RTYPE,NA,T> > {
 	public:
+		typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		typedef times<RTYPE,_NA_,true> OPERATOR ;
+		typedef STORAGE (Times_Vector_Primitive::*METHOD)(int) const ;
 		
 		Times_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
-			lhs(lhs_), rhs(rhs_), op() {}
+			lhs(lhs_), rhs(rhs_), m() {
+				m = Rcpp::traits::is_na<RTYPE>(rhs) ? 
+				&Times_Vector_Primitive::rhs_is_na :
+				&Times_Vector_Primitive::rhs_is_not_na ;
+		}
 		
 		inline STORAGE operator[]( int i ) const {
-			return op.apply( lhs[i], rhs ) ;
+			return (this->*m)(i) ;
 		}
 		
 		inline int size() const { return lhs.size() ; }
-	
 		
 	private:
 		const VEC_TYPE& lhs ;
 		STORAGE rhs ;
-		OPERATOR op ; 
+		METHOD m ;
+		
+		inline STORAGE rhs_is_na(int i) const { return rhs ; }
+		inline STORAGE rhs_is_not_na(int i) const { 
+			STORAGE x = lhs[i] ;
+			return Rcpp::traits::is_na<RTYPE>(x) ? x : (x * rhs) ;
+		}
+		
 	} ;
 	
-	template <int RTYPE, bool LHS_NA, typename LHS_VEC_TYPE, bool RHS_NA, typename RHS_VEC_TYPE >
-	class Times_Vector_Vector : public Rcpp::VectorBase<RTYPE,true, Times_Vector_Vector<RTYPE,LHS_NA,LHS_VEC_TYPE,RHS_NA,RHS_VEC_TYPE> > {
+
+	template <int RTYPE, typename T>
+	class Times_Vector_Primitive<RTYPE,false,T> : public Rcpp::VectorBase<RTYPE,false, Times_Vector_Primitive<RTYPE,false,T> > {
 	public:
+		typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ;
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		typedef times<RTYPE,LHS_NA,RHS_NA> OPERATOR ;
+		typedef STORAGE (Times_Vector_Primitive::*METHOD)(int) const ;
 		
-		Times_Vector_Vector( const LHS_VEC_TYPE& lhs_, const RHS_VEC_TYPE& rhs_ ) : 
-			lhs(lhs_), rhs(rhs_), op() {}
+		Times_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
+			lhs(lhs_), rhs(rhs_), m() {
+				m = Rcpp::traits::is_na<RTYPE>(rhs) ? 
+				&Times_Vector_Primitive::rhs_is_na :
+				&Times_Vector_Primitive::rhs_is_not_na ;
+		}
 		
 		inline STORAGE operator[]( int i ) const {
-			return op.apply( lhs[i], rhs[i] ) ;
+			return (this->*m)(i) ;
 		}
 		
 		inline int size() const { return lhs.size() ; }
 		
 	private:
-		const LHS_VEC_TYPE& lhs ;
-		const RHS_VEC_TYPE& rhs ;
-		OPERATOR op ; 
+		const VEC_TYPE& lhs ;
+		STORAGE rhs ;
+		METHOD m ;
+		
+		inline STORAGE rhs_is_na(int i) const { return rhs ; }
+		inline STORAGE rhs_is_not_na(int i) const { 
+			return rhs * lhs[i] ;
+		}
+		
 	} ;
+	
+	
 }
 }
 
-template <int RTYPE,bool _NA_, typename T>
-inline Rcpp::sugar::Times_Vector_Primitive< RTYPE , _NA_ , Rcpp::VectorBase<RTYPE,_NA_,T> >
+template <int RTYPE,bool NA, typename T>
+inline Rcpp::sugar::Times_Vector_Primitive<RTYPE,NA,T>
 operator*( 
-	const Rcpp::VectorBase<RTYPE,_NA_,T>& lhs, 
+	const Rcpp::VectorBase<RTYPE,NA,T>& lhs, 
 	typename Rcpp::traits::storage_type<RTYPE>::type rhs 
 ) {
-	return Rcpp::sugar::Times_Vector_Primitive<RTYPE,_NA_, Rcpp::VectorBase<RTYPE,_NA_,T> >( lhs, rhs ) ;
+	return Rcpp::sugar::Times_Vector_Primitive<RTYPE,NA,T>( lhs, rhs ) ;
 }
 
 
-template <int RTYPE,bool _NA_, typename T>
-inline Rcpp::sugar::Times_Vector_Primitive< RTYPE , _NA_ , Rcpp::VectorBase<RTYPE,_NA_,T> >
+template <int RTYPE,bool NA, typename T>
+inline Rcpp::sugar::Times_Vector_Primitive< RTYPE , NA , T >
 operator*( 
 	typename Rcpp::traits::storage_type<RTYPE>::type rhs, 
-	const Rcpp::VectorBase<RTYPE,_NA_,T>& lhs
+	const Rcpp::VectorBase<RTYPE,NA,T>& lhs
 ) {
-	return Rcpp::sugar::Times_Vector_Primitive<RTYPE,_NA_, Rcpp::VectorBase<RTYPE,_NA_,T> >( lhs, rhs ) ;
+	return Rcpp::sugar::Times_Vector_Primitive<RTYPE,NA, T >( lhs, rhs ) ;
 }
 
 template <int RTYPE,bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
 inline Rcpp::sugar::Times_Vector_Vector< 
 	RTYPE , 
-	LHS_NA, Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>, 
-	RHS_NA, Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>
+	LHS_NA, LHS_T, 
+	RHS_NA, RHS_T
 	>
 operator*( 
 	const Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>& lhs,
@@ -135,8 +223,8 @@
 ) {
 	return Rcpp::sugar::Times_Vector_Vector<
 		RTYPE, 
-		LHS_NA, Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>,
-		RHS_NA, Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>
+		LHS_NA, LHS_T,
+		RHS_NA, RHS_T
 		>( lhs, rhs ) ;
 }
 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h	2010-06-24 03:43:37 UTC (rev 1700)
+++ pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h	2010-06-24 07:41:51 UTC (rev 1701)
@@ -36,7 +36,9 @@
 		return static_cast<VECTOR&>(*this) ;
 	}
 
-	inline stored_type operator[]( int i) const { return static_cast<const VECTOR*>(this)->operator[](i) ; }
+	inline stored_type operator[]( int i) const { 
+		return static_cast<const VECTOR*>(this)->operator[](i) ;
+	}
 	
 	inline int size() const { return static_cast<const VECTOR*>(this)->size() ; }
 	

Modified: pkg/Rcpp/inst/unitTests/runit.sugar.times.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.times.R	2010-06-24 03:43:37 UTC (rev 1700)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.times.R	2010-06-24 07:41:51 UTC (rev 1701)
@@ -20,13 +20,20 @@
 test.sugar.times <- function( ){
 
 	fx <- cxxfunction( signature( x = "integer" ), '
-		IntegerVector xx(x) ;
+		IntegerVector xx(x) ; 
+		IntegerVector yy = clone<IntegerVector>( xx ) ;
+		yy[0] = NA_INTEGER ;
+		
 		return List::create(
-			xx * 10, 
-			10 * xx, 
-			xx * xx, 
-			xx * xx * xx
-			) ;
+            xx * 10, 
+            10 * xx, 
+            xx * xx, 
+            xx * xx * xx, 
+            xx * yy, 
+            yy * 10, 
+            10 * yy, 
+            NA_INTEGER * xx
+        ) ;
 	', plugin = "Rcpp" )
 	
 	checkEquals( fx(1:10) , 
@@ -34,11 +41,15 @@
 			10L*(1:10),
 			10L*(1:10),
 			(1:10)*(1:10), 
-			(1:10)*(1:10)*(1:10)
+			(1:10)*(1:10)*(1:10), 
+			c(NA,(2:10)*(2:10)), 
+			c(NA,10L*(2:10)), 
+			c(NA,10L*(2:10)), 
+			rep( NA_integer_, 10L )
 		)
 	)
 }
-
+                         
 test.sugar.divides <- function( ){
 
 	fx <- cxxfunction( signature( x = "numeric" ), '



More information about the Rcpp-commits mailing list