[Rcpp-commits] r2202 - pkg/Rcpp/inst/include/Rcpp/sugar/operators

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Sep 26 21:37:49 CEST 2010


Author: romain
Date: 2010-09-26 21:37:49 +0200 (Sun, 26 Sep 2010)
New Revision: 2202

Modified:
   pkg/Rcpp/inst/include/Rcpp/sugar/operators/minus.h
Log:
first pass at improving operator- (remove use of pointer to member functions) 

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/operators/minus.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/operators/minus.h	2010-09-26 19:27:12 UTC (rev 2201)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/operators/minus.h	2010-09-26 19:37:49 UTC (rev 2202)
@@ -127,19 +127,14 @@
 	public:
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
 		typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
-		typedef STORAGE (Minus_Vector_Primitive::*METHOD)(int) const ;
 		
 		Minus_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
-			lhs(lhs_), rhs(rhs_){
-			
-			m = Rcpp::traits::is_na<RTYPE>(rhs) ? 
-				&Minus_Vector_Primitive::rhs_is_na :
-				&Minus_Vector_Primitive::rhs_is_not_na ;
-			
-		}
+			lhs(lhs_), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
 		
 		inline STORAGE operator[]( int i ) const {
-			return (this->*m)(i) ;
+			if( rhs_na ) return rhs ;
+			STORAGE x = lhs[i] ;
+			return Rcpp::traits::is_na<RTYPE>(x) ? x : (x - rhs) ;
 		}
 		
 		inline int size() const { return lhs.size() ; }
@@ -147,14 +142,8 @@
 	private:
 		const VEC_TYPE& lhs ;
 		STORAGE rhs ;
-		METHOD m ;
+		bool rhs_na ;
 		
-		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) ;
-		}
-		
 	} ;
 	
 
@@ -163,19 +152,14 @@
 	public:
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
 		typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ;
-		typedef STORAGE (Minus_Vector_Primitive::*METHOD)(int) const ;
 		
 		Minus_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
-			lhs(lhs_), rhs(rhs_){
-
-			m = Rcpp::traits::is_na<RTYPE>(rhs) ? 
-				&Minus_Vector_Primitive::rhs_is_na :
-				&Minus_Vector_Primitive::rhs_is_not_na ;
-				
-		}
+			lhs(lhs_), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
 		
 		inline STORAGE operator[]( int i ) const {
-			return (this->*m)(i) ;
+			if( rhs_na ) return rhs ;
+			STORAGE x = rhs[i] ;
+			return Rcpp::traits::is_na<RTYPE>(x) ? x : (lhs - x) ;
 		}
 		
 		inline int size() const { return lhs.size() ; }
@@ -183,14 +167,8 @@
 	private:
 		const VEC_TYPE& lhs ;
 		STORAGE rhs ;
-		METHOD m ;
+		bool rhs_na ;
 		
-		inline STORAGE rhs_is_na(int i) const { return rhs ; }
-		inline STORAGE rhs_is_not_na(int i) const { 
-			STORAGE x = rhs[i] ;
-			return Rcpp::traits::is_na<RTYPE>(x) ? x : (lhs - x) ;
-		}
-		
 	} ;
 
 	
@@ -206,16 +184,11 @@
 		typedef STORAGE (Minus_Primitive_Vector::*METHOD)(int) const ;
 		
 		Minus_Primitive_Vector( STORAGE lhs_, const VEC_TYPE& rhs_ ) : 
-			lhs(lhs_), rhs(rhs_) {
-				
-			m = Rcpp::traits::is_na<RTYPE>(lhs) ? 
-				&Minus_Primitive_Vector::lhs_is_na :
-				&Minus_Primitive_Vector::lhs_is_not_na ;
-
-		}
+			lhs(lhs_), rhs(rhs_), lhs_na( Rcpp::traits::is_na<RTYPE>(lhs_) ) {}
 		
 		inline STORAGE operator[]( int i ) const {
-			return (this->*m)(i) ;
+			if( lhs_na ) return lhs ;
+			return lhs - rhs[i] ;
 		}
 		
 		inline int size() const { return rhs.size() ; }
@@ -224,13 +197,8 @@
 	private:
 		STORAGE lhs ;
 		const VEC_TYPE& rhs ;
-		METHOD m ;
+		bool lhs_na ;
 		
-		inline STORAGE lhs_is_na(int i) const { return lhs ; }
-		inline STORAGE lhs_is_not_na(int i) const { 
-			return lhs - rhs[i] ;
-		}
-		
 	} ;
 
 	
@@ -242,31 +210,20 @@
 		typedef STORAGE (Minus_Primitive_Vector::*METHOD)(int) const ;
 		
 		Minus_Primitive_Vector( STORAGE lhs_, const VEC_TYPE& rhs_ ) : 
-			lhs(lhs_), rhs(rhs_) {
-				
-			m = Rcpp::traits::is_na<RTYPE>(rhs) ? 
-				&Minus_Primitive_Vector::lhs_is_na :
-				&Minus_Primitive_Vector::lhs_is_not_na ;
-
-		}
+			lhs(lhs_), rhs(rhs_), lhs_na( Rcpp::traits::is_na<RTYPE>(lhs_) ) {}
 		
 		inline STORAGE operator[]( int i ) const {
-			return (this->*m)(i) ;
+			if( lhs_na ) return lhs ;
+		    return lhs - rhs[i] ;
 		}
 		
 		inline int size() const { return rhs.size() ; }
-	
 		
 	private:
 		STORAGE lhs ;
 		const VEC_TYPE& rhs ;
-		METHOD m ;
+		bool rhs_na ;
 		
-		inline STORAGE lhs_is_na(int i) const { return lhs ; }
-		inline STORAGE lhs_is_not_na(int i) const { 
-			return lhs - rhs[i] ;
-		}
-		
 	} ;
 
 



More information about the Rcpp-commits mailing list