[Rcpp-commits] r2173 - in pkg/Rcpp/inst: examples/ConvolveBenchmarks include/Rcpp/sugar/nona include/Rcpp/sugar/operators

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Sep 25 12:31:03 CEST 2010


Author: romain
Date: 2010-09-25 12:31:02 +0200 (Sat, 25 Sep 2010)
New Revision: 2173

Modified:
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve11_cpp.cpp
   pkg/Rcpp/inst/include/Rcpp/sugar/nona/nona.h
   pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h
Log:
introduce nona(double) which speed things further

Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve11_cpp.cpp
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve11_cpp.cpp	2010-09-25 10:19:44 UTC (rev 2172)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve11_cpp.cpp	2010-09-25 10:31:02 UTC (rev 2173)
@@ -14,7 +14,7 @@
     
     Range r( 0, n_xb-1 );
     for(int i=0; i<n_xa; i++, r++){
-    	xab[ r ] += xa[i] * nona(xb) ;
+    	xab[ r ] += nona(xa[i]) * nona(xb) ;
     }
     return xab ;
 }

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/nona/nona.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/nona/nona.h	2010-09-25 10:19:44 UTC (rev 2172)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/nona/nona.h	2010-09-25 10:31:02 UTC (rev 2173)
@@ -61,7 +61,7 @@
     class NonaPrimitive {
     public:
         NonaPrimitive( T t) : x(t){}
-        operator T(){ return x ; }
+        inline operator T(){ return x ; }
         
     private:
         T x ;

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h	2010-09-25 10:19:44 UTC (rev 2172)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h	2010-09-25 10:31:02 UTC (rev 2173)
@@ -207,7 +207,91 @@
 		
 	} ;
 	
+
+
 	
+	
+	
+	// Vector * nona(primitive)
+	
+	template <int RTYPE, bool NA, typename T>
+	class Times_Vector_Primitive_nona : public Rcpp::VectorBase<RTYPE,true, Times_Vector_Primitive_nona<RTYPE,NA,T> > {
+	public:
+		typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
+		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+		
+		Times_Vector_Primitive_nona( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
+			lhs(lhs_.get_ref()), rhs(rhs_)
+			{}
+		
+		inline STORAGE operator[]( int i ) const {
+			STORAGE x = lhs[i] ;
+			return Rcpp::traits::is_na<RTYPE>(x) ? x : (x * rhs) ;
+		}
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const T& lhs ;
+		STORAGE rhs ;
+		
+	} ;
+
+	
+	// very special version that is used when the vector expression is 
+	// actually a vector. In that case we use its iterator (usually pointer)
+	// directly instead of adding the overhead of operator[]
+	template <int RTYPE>
+	class Times_Vector_Primitive_nona<RTYPE,true,Rcpp::Vector<RTYPE> > : 
+	    public Rcpp::VectorBase<RTYPE,true, Times_Vector_Primitive_nona<RTYPE,true,Rcpp::Vector<RTYPE> > > {
+	public:
+	    
+	    typedef typename Rcpp::Vector<RTYPE>::iterator iterator ;
+		typedef typename Rcpp::VectorBase<RTYPE,true,Rcpp::Vector<RTYPE> > VEC_TYPE ;
+		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+		
+		Times_Vector_Primitive_nona( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
+			lhs(lhs_.get_ref().begin())
+			, rhs(rhs_)
+			, n( lhs_.size() )
+			{}
+		
+		inline STORAGE operator[]( int i ) const {
+			STORAGE x = lhs[i] ;
+			return Rcpp::traits::is_na<RTYPE>(x) ? x : (x * rhs) ;
+		}
+		
+		inline int size() const { return n ; }
+		
+	private:
+		iterator lhs ;
+	    STORAGE rhs ;
+		int n ;
+		
+	} ;
+	
+	
+	template <int RTYPE, typename T>
+	class Times_Vector_Primitive_nona<RTYPE,false,T> : public Rcpp::VectorBase<RTYPE,false, Times_Vector_Primitive_nona<RTYPE,false,T> > {
+	public:
+		typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ;
+		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+		
+		Times_Vector_Primitive_nona( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
+			lhs(lhs_.get_ref()), rhs(rhs_) {}
+		
+		inline STORAGE operator[]( int i ) const {
+			return rhs * lhs[i] ;
+		}
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const T& lhs ;
+		STORAGE rhs ;
+		
+	} ;
+	
 }
 }
 
@@ -230,6 +314,27 @@
 	return Rcpp::sugar::Times_Vector_Primitive<RTYPE,NA, T >( lhs, rhs ) ;
 }
 
+
+
+template <int RTYPE,bool NA, typename T>
+inline Rcpp::sugar::Times_Vector_Primitive_nona<RTYPE,NA,T>
+operator*( 
+	const Rcpp::VectorBase<RTYPE,NA,T>& lhs, 
+	typename Rcpp::sugar::NonaPrimitive< typename Rcpp::traits::storage_type<RTYPE>::type > rhs 
+) {
+	return Rcpp::sugar::Times_Vector_Primitive_nona<RTYPE,NA,T>( lhs, rhs ) ;
+}
+
+template <int RTYPE,bool NA, typename T>
+inline Rcpp::sugar::Times_Vector_Primitive_nona< RTYPE , NA , T >
+operator*( 
+	typename Rcpp::sugar::NonaPrimitive< typename Rcpp::traits::storage_type<RTYPE>::type > rhs, 
+	const Rcpp::VectorBase<RTYPE,NA,T>& lhs
+) {
+	return Rcpp::sugar::Times_Vector_Primitive_nona<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 , 



More information about the Rcpp-commits mailing list