[Rcpp-commits] r2819 - in pkg/Rcpp: . inst/include/Rcpp/sugar/operators inst/unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Dec 23 16:47:46 CET 2010


Author: romain
Date: 2010-12-23 16:47:46 +0100 (Thu, 23 Dec 2010)
New Revision: 2819

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/sugar/operators/minus.h
   pkg/Rcpp/inst/unitTests/runit.support.R
Log:
more efficient operator-

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2010-12-23 11:57:49 UTC (rev 2818)
+++ pkg/Rcpp/ChangeLog	2010-12-23 15:47:46 UTC (rev 2819)
@@ -9,6 +9,9 @@
 
 	* inst/include/Rcpp/sugar/operators/divides.h: More efficient operator/ for 
 	REALSXP sugar expressions, avoiding unnecessary NA tests.
+
+	* inst/include/Rcpp/sugar/operators/minus.h: More efficient operator- for 
+	REALSXP sugar expressions, avoiding unnecessary NA tests.
 	
 2010-12-22  Romain Francois <romain at r-enthusiasts.com>
 

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/operators/minus.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/operators/minus.h	2010-12-23 11:57:49 UTC (rev 2818)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/operators/minus.h	2010-12-23 15:47:46 UTC (rev 2819)
@@ -26,12 +26,15 @@
 namespace sugar{
 
 	template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T >
-	class Minus_Vector_Vector : public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
+	class Minus_Vector_Vector : 
+	    public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
 	public:
 		typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
 		typedef typename Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		
+		typedef typename Rcpp::traits::Extractor< RTYPE, LHS_NA, LHS_T>::type LHS_EXT ;
+		typedef typename Rcpp::traits::Extractor< RTYPE, RHS_NA, RHS_T>::type RHS_EXT ;
+				
 		Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
 			lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
 		
@@ -45,9 +48,32 @@
 		inline int size() const { return lhs.size() ; }
 		
 	private:
-		const LHS_T& lhs ;
-		const RHS_T& rhs ;
+		const LHS_EXT& lhs ;
+		const RHS_EXT& rhs ;
 	} ;
+	// RTYPE = REALSXP
+	template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T >
+	class Minus_Vector_Vector<REALSXP,LHS_NA,LHS_T,RHS_NA,RHS_T> : 
+	    public Rcpp::VectorBase<REALSXP,true, Minus_Vector_Vector<REALSXP,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
+	public:
+		typedef typename Rcpp::VectorBase<REALSXP,LHS_NA,LHS_T> LHS_TYPE ;
+		typedef typename Rcpp::VectorBase<REALSXP,RHS_NA,RHS_T> RHS_TYPE ;
+		typedef typename Rcpp::traits::Extractor<REALSXP, LHS_NA, LHS_T>::type LHS_EXT ;
+		typedef typename Rcpp::traits::Extractor<REALSXP, RHS_NA, RHS_T>::type RHS_EXT ;
+				
+		Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
+			lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
+		
+		inline double operator[]( int i ) const {
+		    return lhs[i] - rhs[i] ;
+		}
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const LHS_EXT& lhs ;
+		const RHS_EXT& rhs ;
+	} ;
 	
 	
 	template <int RTYPE, typename LHS_T, bool RHS_NA, typename RHS_T >
@@ -56,6 +82,8 @@
 		typedef typename Rcpp::VectorBase<RTYPE,false,LHS_T> LHS_TYPE ;
 		typedef typename Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+		typedef typename Rcpp::traits::Extractor<RTYPE, false, LHS_T>::type LHS_EXT ;
+		typedef typename Rcpp::traits::Extractor<RTYPE, RHS_NA, RHS_T>::type RHS_EXT ;
 		
 		Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
 			lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
@@ -69,9 +97,32 @@
 		inline int size() const { return lhs.size() ; }
 		
 	private:
-		const LHS_T& lhs ;
-		const RHS_T& rhs ;
+		const LHS_EXT& lhs ;
+		const RHS_EXT& rhs ;
 	} ;
+	// RTYPE = REALSXP
+	template <typename LHS_T, bool RHS_NA, typename RHS_T >
+	class Minus_Vector_Vector<REALSXP,false,LHS_T,RHS_NA,RHS_T> : 
+	    public Rcpp::VectorBase<REALSXP,true, Minus_Vector_Vector<REALSXP,false,LHS_T,RHS_NA,RHS_T> > {
+	public:
+		typedef typename Rcpp::VectorBase<REALSXP,false,LHS_T> LHS_TYPE ;
+		typedef typename Rcpp::VectorBase<REALSXP,RHS_NA,RHS_T> RHS_TYPE ;
+		typedef typename Rcpp::traits::Extractor<REALSXP, false, LHS_T>::type LHS_EXT ;
+		typedef typename Rcpp::traits::Extractor<REALSXP, RHS_NA, RHS_T>::type RHS_EXT ;
+		
+		Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
+			lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
+		
+		inline double operator[]( int i ) const {
+			return lhs[i] - rhs[i] ;
+		}
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const LHS_EXT& lhs ;
+		const RHS_EXT& rhs ;
+	} ;
 
 	
 	template <int RTYPE, bool LHS_NA, typename LHS_T, typename RHS_T >
@@ -80,7 +131,9 @@
 		typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
 		typedef typename Rcpp::VectorBase<RTYPE,false,RHS_T> RHS_TYPE ;
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		
+		typedef typename Rcpp::traits::Extractor<RTYPE, LHS_NA, LHS_T>::type LHS_EXT ;
+		typedef typename Rcpp::traits::Extractor<RTYPE, false, RHS_T>::type RHS_EXT ;
+
 		Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
 			lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
 		
@@ -93,18 +146,44 @@
 		inline int size() const { return lhs.size() ; }
 		
 	private:
-		const LHS_T& lhs ;
-		const RHS_T& rhs ;
+		const LHS_EXT& lhs ;
+		const RHS_EXT& rhs ;
 	} ;
+	// RTYPE = REALSXP
+	template <bool LHS_NA, typename LHS_T, typename RHS_T >
+	class Minus_Vector_Vector<REALSXP,LHS_NA,LHS_T,false,RHS_T> : 
+	    public Rcpp::VectorBase<REALSXP,true, Minus_Vector_Vector<REALSXP,LHS_NA,LHS_T,false,RHS_T> > {
+	public:
+		typedef typename Rcpp::VectorBase<REALSXP,LHS_NA,LHS_T> LHS_TYPE ;
+		typedef typename Rcpp::VectorBase<REALSXP,false,RHS_T> RHS_TYPE ;
+		typedef typename Rcpp::traits::Extractor<REALSXP, LHS_NA, LHS_T>::type LHS_EXT ;
+		typedef typename Rcpp::traits::Extractor<REALSXP, false, RHS_T>::type RHS_EXT ;
+
+		Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
+			lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
+		
+		inline double operator[]( int i ) const {
+			return lhs[i] - rhs[i] ;
+		}
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const LHS_EXT& lhs ;
+		const RHS_EXT& rhs ;
+	} ;
 	
 	
 	template <int RTYPE, typename LHS_T, typename RHS_T >
-	class Minus_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> : public Rcpp::VectorBase<RTYPE,false, Minus_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> > {
+	class Minus_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> : 
+	    public Rcpp::VectorBase<RTYPE,false, Minus_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> > {
 	public:
 		typedef typename Rcpp::VectorBase<RTYPE,false,LHS_T> LHS_TYPE ;
 		typedef typename Rcpp::VectorBase<RTYPE,false,RHS_T> RHS_TYPE ;
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		
+		typedef typename Rcpp::traits::Extractor<RTYPE, false, LHS_T>::type LHS_EXT ;
+		typedef typename Rcpp::traits::Extractor<RTYPE, false, RHS_T>::type RHS_EXT ;
+
 		Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
 			lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
 		
@@ -115,20 +194,44 @@
 		inline int size() const { return lhs.size() ; }
 		
 	private:
-		const LHS_T& lhs ;
-		const RHS_T& rhs ;
+		const LHS_EXT& lhs ;
+		const RHS_EXT& rhs ;
 	} ;
+	template <typename LHS_T, typename RHS_T >
+	class Minus_Vector_Vector<REALSXP,false,LHS_T,false,RHS_T> : 
+	    public Rcpp::VectorBase<REALSXP,false, Minus_Vector_Vector<REALSXP,false,LHS_T,false,RHS_T> > {
+	public:
+		typedef typename Rcpp::VectorBase<REALSXP,false,LHS_T> LHS_TYPE ;
+		typedef typename Rcpp::VectorBase<REALSXP,false,RHS_T> RHS_TYPE ;
+		typedef typename Rcpp::traits::Extractor<REALSXP, false, LHS_T>::type LHS_EXT ;
+		typedef typename Rcpp::traits::Extractor<REALSXP, false, RHS_T>::type RHS_EXT ;
+
+		Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
+			lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
+		
+		inline double operator[]( int i ) const {
+			return lhs[i] - rhs[i] ;
+		}
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const LHS_EXT& lhs ;
+		const RHS_EXT& rhs ;
+	} ;
 	
 	
 	
 	
 	
 	template <int RTYPE, bool NA, typename T>
-	class Minus_Vector_Primitive : public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Primitive<RTYPE,NA,T> > {
+	class Minus_Vector_Primitive : 
+	    public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Primitive<RTYPE,NA,T> > {
 	public:
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
 		typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
-		
+		typedef typename Rcpp::traits::Extractor<RTYPE,NA,T>::type VEC_EXT ;
+
 		Minus_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
 			lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
 		
@@ -141,19 +244,40 @@
 		inline int size() const { return lhs.size() ; }
 		
 	private:
-		const T& lhs ;
+		const VEC_EXT& lhs ;
 		STORAGE rhs ;
 		bool rhs_na ;
+	} ;
+	template <bool NA, typename T>
+	class Minus_Vector_Primitive<REALSXP,NA,T> : 
+	    public Rcpp::VectorBase<REALSXP,true, Minus_Vector_Primitive<REALSXP,NA,T> > {
+	public:
+		typedef typename Rcpp::VectorBase<REALSXP,NA,T> VEC_TYPE ;
+		typedef typename Rcpp::traits::Extractor<REALSXP,NA,T>::type VEC_EXT ;
+
+		Minus_Vector_Primitive( const VEC_TYPE& lhs_, double rhs_ ) : 
+			lhs(lhs_.get_ref()), rhs(rhs_){}
 		
+		inline double operator[]( int i ) const {
+			return lhs[i] - rhs ;
+		}
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const VEC_EXT& lhs ;
+		double rhs ;
 	} ;
 	
 
 	template <int RTYPE, typename T>
-	class Minus_Vector_Primitive<RTYPE,false,T> : public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Primitive<RTYPE,false,T> > {
+	class Minus_Vector_Primitive<RTYPE,false,T> : 
+	    public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Primitive<RTYPE,false,T> > {
 	public:
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
 		typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ;
-		
+		typedef typename Rcpp::traits::Extractor<RTYPE,false,T>::type VEC_EXT ;
+
 		Minus_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
 			lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
 		
@@ -166,10 +290,29 @@
 		inline int size() const { return lhs.size() ; }
 		
 	private:
-		const T& lhs ;
+		const VEC_EXT& lhs ;
 		STORAGE rhs ;
 		bool rhs_na ;
+	} ;
+	template <typename T>
+	class Minus_Vector_Primitive<REALSXP,false,T> : 
+	    public Rcpp::VectorBase<REALSXP,true, Minus_Vector_Primitive<REALSXP,false,T> > {
+	public:
+		typedef typename Rcpp::VectorBase<REALSXP,false,T> VEC_TYPE ;
+		typedef typename Rcpp::traits::Extractor<REALSXP,false,T>::type VEC_EXT ;
+
+		Minus_Vector_Primitive( const VEC_TYPE& lhs_, double rhs_ ) : 
+			lhs(lhs_.get_ref()), rhs(rhs_){}
 		
+		inline double operator[]( int i ) const {
+			return lhs[i] - rhs ;
+		}
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const VEC_EXT& lhs ;
+		double rhs ;
 	} ;
 
 	
@@ -178,11 +321,13 @@
 	
 	
 	template <int RTYPE, bool NA, typename T>                                                   
-	class Minus_Primitive_Vector : public Rcpp::VectorBase<RTYPE,true, Minus_Primitive_Vector<RTYPE,NA,T> > {
+	class Minus_Primitive_Vector : 
+	    public Rcpp::VectorBase<RTYPE,true, Minus_Primitive_Vector<RTYPE,NA,T> > {
 	public:
 		typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ; 
-		
+		typedef typename Rcpp::traits::Extractor<RTYPE,NA,T>::type VEC_EXT ;
+
 		Minus_Primitive_Vector( STORAGE lhs_, const VEC_TYPE& rhs_ ) : 
 			lhs(lhs_), rhs(rhs_.get_ref()), lhs_na( Rcpp::traits::is_na<RTYPE>(lhs_) ) {}
 		
@@ -190,24 +335,43 @@
 			if( lhs_na ) return lhs ;
 			return lhs - rhs[i] ;
 		}
-		
 		inline int size() const { return rhs.size() ; }
-	
 		
 	private:
 		STORAGE lhs ;
-		const T& rhs ;
+		const VEC_EXT& rhs ;
 		bool lhs_na ;
+	} ;
+	template <bool NA, typename T>                                                   
+	class Minus_Primitive_Vector<REALSXP,NA,T> : 
+	    public Rcpp::VectorBase<REALSXP,true, Minus_Primitive_Vector<REALSXP,NA,T> > {
+	public:
+		typedef typename Rcpp::VectorBase<REALSXP,NA,T> VEC_TYPE ;
+		typedef typename Rcpp::traits::Extractor<REALSXP,NA,T>::type VEC_EXT ;
+
+		Minus_Primitive_Vector( double lhs_, const VEC_TYPE& rhs_ ) : 
+			lhs(lhs_), rhs(rhs_.get_ref()){}
 		
+		inline double operator[]( int i ) const {
+			return lhs - rhs[i] ;
+		}
+		inline int size() const { return rhs.size() ; }
+		
+	private:
+		double lhs ;
+		const VEC_EXT& rhs ;
 	} ;
+	
 
 	
 	template <int RTYPE, typename T>                                                   
-	class Minus_Primitive_Vector<RTYPE,false,T> : public Rcpp::VectorBase<RTYPE,true, Minus_Primitive_Vector<RTYPE,false,T> > {
+	class Minus_Primitive_Vector<RTYPE,false,T> : 
+	    public Rcpp::VectorBase<RTYPE,true, Minus_Primitive_Vector<RTYPE,false,T> > {
 	public:
 		typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ;
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ; 
-		
+		typedef typename Rcpp::traits::Extractor<REALSXP,false,T>::type VEC_EXT ;
+
 		Minus_Primitive_Vector( STORAGE lhs_, const VEC_TYPE& rhs_ ) : 
 			lhs(lhs_), rhs(rhs_.get_ref()), lhs_na( Rcpp::traits::is_na<RTYPE>(lhs_) ) {}
 		
@@ -220,12 +384,32 @@
 		
 	private:
 		STORAGE lhs ;
-		const T& rhs ;
+		const VEC_EXT& rhs ;
 		bool lhs_na ;
 		
 	} ;
+	template <typename T>                                                   
+	class Minus_Primitive_Vector<REALSXP,false,T> : 
+	    public Rcpp::VectorBase<REALSXP,true, Minus_Primitive_Vector<REALSXP,false,T> > {
+	public:
+		typedef typename Rcpp::VectorBase<REALSXP,false,T> VEC_TYPE ;
+		typedef typename Rcpp::traits::Extractor<REALSXP,false,T>::type VEC_EXT ;
 
+		Minus_Primitive_Vector( double lhs_, const VEC_TYPE& rhs_ ) : 
+			lhs(lhs_), rhs(rhs_.get_ref()){}
+		
+		inline double operator[]( int i ) const {
+			return lhs - rhs[i] ;
+		}
+		
+		inline int size() const { return rhs.size() ; }
+		
+	private:
+		double lhs ;
+		const VEC_EXT& rhs ;
+	} ;
 
+
 }
 }
 

Modified: pkg/Rcpp/inst/unitTests/runit.support.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.support.R	2010-12-23 11:57:49 UTC (rev 2818)
+++ pkg/Rcpp/inst/unitTests/runit.support.R	2010-12-23 15:47:46 UTC (rev 2819)
@@ -36,7 +36,6 @@
            NA_REAL * 1.0,
            1.0 * NA_REAL 
            ); 
-        
        '), 
      "divides_REALSXP" = list( 
         signature(), 
@@ -46,8 +45,17 @@
            NA_REAL / 1.0,
            1.0 / NA_REAL 
            ); 
-        
         '
+     ), 
+     "minus_REALSXP" = list( 
+        signature(), 
+        '
+        return List::create( 
+           NA_REAL - NA_REAL, 
+           NA_REAL - 1.0,
+           1.0 - NA_REAL 
+           ); 
+        '
      )
     )
 }
@@ -83,3 +91,11 @@
         msg = " REALSXP / REALSXP" )
 }
 
+test.minus.REALSXP <- function(){
+    fun <- .rcpp.support$minus_REALSXP
+    checkEquals( 
+        fun(), 
+        list(NA_real_,NA_real_,NA_real_) , 
+        msg = " REALSXP - REALSXP" )
+}
+



More information about the Rcpp-commits mailing list