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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 24 10:30:48 CEST 2010


Author: romain
Date: 2010-06-24 10:30:47 +0200 (Thu, 24 Jun 2010)
New Revision: 1704

Modified:
   pkg/Rcpp/inst/include/Rcpp/sugar/operators/minus.h
Log:
similar adjustmnets for operator-

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/operators/minus.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/operators/minus.h	2010-06-24 08:02:35 UTC (rev 1703)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/operators/minus.h	2010-06-24 08:30:47 UTC (rev 1704)
@@ -25,74 +25,197 @@
 namespace Rcpp{
 namespace sugar{
 
-	// TODO: what happens in the limits, see what R does
-	template <int RTYPE,bool LHS_NA, bool RHS_NA>
-	class minus{
+	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> > {
 	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 ;
-		inline STORAGE apply( STORAGE lhs, STORAGE rhs) const {
-			return traits::is_na<RTYPE>(lhs) ? lhs : ( traits::is_na<RTYPE>(rhs) ? rhs : (lhs - rhs) ) ;
+		
+		Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
+			lhs(lhs_), rhs(rhs_) {}
+		
+		inline STORAGE operator[]( int i ) const {
+			STORAGE x = lhs[i] ; 
+			if( Rcpp::traits::is_na<RTYPE>( x ) ) return x ;
+			STORAGE y = rhs[i] ; 
+			return Rcpp::traits::is_na<RTYPE>( y ) ? y : ( x - y ) ;
 		}
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const LHS_TYPE& lhs ;
+		const RHS_TYPE& rhs ;
 	} ;
-	template <int RTYPE,bool RHS_NA>
-	class minus<RTYPE,false,RHS_NA>{
+	
+	
+	template <int RTYPE, typename LHS_T, bool RHS_NA, typename RHS_T >
+	class Minus_Vector_Vector<RTYPE,false,LHS_T,RHS_NA,RHS_T> : public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Vector<RTYPE,false,LHS_T,RHS_NA,RHS_T> > {
 	public:
+		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 ;
-		inline STORAGE apply( STORAGE lhs, STORAGE rhs) const {
-			return traits::is_na<RTYPE>(rhs) ? rhs : (lhs - rhs);
+		
+		Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
+			lhs(lhs_), rhs(rhs_) {}
+		
+		inline STORAGE operator[]( int i ) const {
+			STORAGE y = rhs[i] ; 
+			if( Rcpp::traits::is_na<RTYPE>( y ) ) return y ;
+			return lhs[i] - y ;
 		}
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const LHS_TYPE& lhs ;
+		const RHS_TYPE& rhs ;
 	} ;
-	template <int RTYPE,bool LHS_NA>
-	class minus<RTYPE,LHS_NA,false>{
+
+	
+	template <int RTYPE, bool LHS_NA, typename LHS_T, typename RHS_T >
+	class Minus_Vector_Vector<RTYPE,LHS_NA,LHS_T,false,RHS_T> : public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Vector<RTYPE,LHS_NA,LHS_T,false,RHS_T> > {
 	public:
+		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 ;
-		inline STORAGE apply( STORAGE lhs, STORAGE rhs) const {
-			return traits::is_na<RTYPE>(lhs) ? lhs : (lhs - rhs);
+		
+		Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
+			lhs(lhs_), rhs(rhs_) {}
+		
+		inline STORAGE operator[]( int i ) const {
+			STORAGE x = lhs[i] ; 
+			if( Rcpp::traits::is_na<RTYPE>( x ) ) return x ;
+			return x - rhs[i] ;
 		}
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const LHS_TYPE& lhs ;
+		const RHS_TYPE& rhs ;
 	} ;
-	template <int RTYPE>
-	class minus<RTYPE,false,false>{
+	
+	
+	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> > {
 	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 ;
-		inline STORAGE apply( STORAGE lhs, STORAGE rhs) const {
-			return lhs - rhs;
+		
+		Minus_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 Minus_Vector_Primitive : public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Primitive<RTYPE,_NA_,VEC_TYPE> > {
+	
+	
+	template <int RTYPE, bool NA, typename 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 minus<RTYPE,_NA_,true> OPERATOR ;
+		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_), op() {}
+			lhs(lhs_), rhs(rhs_){
+			
+			m = Rcpp::traits::is_na<RTYPE>(rhs) ? 
+				&Minus_Vector_Primitive::rhs_is_na :
+				&Minus_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 _NA_, typename VEC_TYPE>
-	class Minus_Primitive_Vector : public Rcpp::VectorBase<RTYPE,true, Minus_Primitive_Vector<RTYPE,_NA_,VEC_TYPE> > {
+
+	template <int RTYPE, typename 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 minus<RTYPE,_NA_,true> OPERATOR ;
+		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 ;
+				
+		}
+		
+		inline STORAGE operator[]( int i ) const {
+			return (this->*m)(i) ;
+		}
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		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 { 
+			STORAGE x = rhs[i] ;
+			return Rcpp::traits::is_na<RTYPE>(x) ? x : (lhs - x) ;
+		}
+		
+	} ;
+
+	
+	
+	
+	
+	
+	template <int RTYPE, bool NA, typename 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 STORAGE (Minus_Primitive_Vector::*METHOD)(int) const ;
+		
 		Minus_Primitive_Vector( STORAGE lhs_, const VEC_TYPE& rhs_ ) : 
-			lhs(lhs_), rhs(rhs_), op() {}
+			lhs(lhs_), rhs(rhs_) {
+				
+			m = Rcpp::traits::is_na<RTYPE>(lhs) ? 
+				&Minus_Primitive_Vector::lhs_is_na :
+				&Minus_Primitive_Vector::lhs_is_not_na ;
+
+		}
 		
 		inline STORAGE operator[]( int i ) const {
-			return op.apply( lhs, rhs[i] ) ;
+			return (this->*m)(i) ;
 		}
 		
 		inline int size() const { return rhs.size() ; }
@@ -101,57 +224,79 @@
 	private:
 		STORAGE lhs ;
 		const VEC_TYPE& rhs ;
-		OPERATOR op ; 
+		METHOD m ;
+		
+		inline STORAGE lhs_is_na(int i) const { return lhs ; }
+		inline STORAGE lhs_is_not_na(int i) const { 
+			return lhs - rhs[i] ;
+		}
+		
 	} ;
 
-
-	template <int RTYPE, bool LHS_NA, typename LHS_VEC_TYPE, bool RHS_NA, typename RHS_VEC_TYPE >
-	class Minus_Vector_Vector : public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Vector<RTYPE,LHS_NA,LHS_VEC_TYPE,RHS_NA,RHS_VEC_TYPE> > {
+	
+	template <int RTYPE, typename T>                                                   
+	class Minus_Primitive_Vector<RTYPE,false,T> : public Rcpp::VectorBase<RTYPE,true, Minus_Primitive_Vector<RTYPE,false,T> > {
 	public:
-		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		typedef minus<RTYPE,LHS_NA,RHS_NA> OPERATOR ;
+		typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ;
+		typedef typename traits::storage_type<RTYPE>::type STORAGE ; 
+		typedef STORAGE (Minus_Primitive_Vector::*METHOD)(int) const ;
 		
-		Minus_Vector_Vector( const LHS_VEC_TYPE& lhs_, const RHS_VEC_TYPE& rhs_ ) : 
-			lhs(lhs_), rhs(rhs_), op() {}
+		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 ;
+
+		}
 		
 		inline STORAGE operator[]( int i ) const {
-			return op.apply( lhs[i], rhs[i] ) ;
+			return (this->*m)(i) ;
 		}
 		
-		inline int size() const { return lhs.size() ; }
+		inline int size() const { return rhs.size() ; }
+	
 		
 	private:
-		const LHS_VEC_TYPE& lhs ;
-		const RHS_VEC_TYPE& rhs ;
-		OPERATOR op ; 
+		STORAGE lhs ;
+		const VEC_TYPE& rhs ;
+		METHOD m ;
+		
+		inline STORAGE lhs_is_na(int i) const { return lhs ; }
+		inline STORAGE lhs_is_not_na(int i) const { 
+			return lhs - rhs[i] ;
+		}
+		
 	} ;
+
+
 }
 }
 
-template <int RTYPE,bool _NA_, typename T>
-inline Rcpp::sugar::Minus_Vector_Primitive< RTYPE , _NA_ , Rcpp::VectorBase<RTYPE,_NA_,T> >
+template <int RTYPE,bool NA, typename T>
+inline Rcpp::sugar::Minus_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::Minus_Vector_Primitive<RTYPE,_NA_, Rcpp::VectorBase<RTYPE,_NA_,T> >( lhs, rhs ) ;
+	return Rcpp::sugar::Minus_Vector_Primitive<RTYPE,NA,T>( lhs, rhs ) ;
 }
 
 
-template <int RTYPE,bool _NA_, typename T>
-inline Rcpp::sugar::Minus_Primitive_Vector< RTYPE , _NA_ , Rcpp::VectorBase<RTYPE,_NA_,T> >
+template <int RTYPE,bool NA, typename T>
+inline Rcpp::sugar::Minus_Primitive_Vector< RTYPE , NA,T>
 operator-( 
 	typename Rcpp::traits::storage_type<RTYPE>::type lhs, 
-	const Rcpp::VectorBase<RTYPE,_NA_,T>& rhs
+	const Rcpp::VectorBase<RTYPE,NA,T>& rhs
 ) {
-	return Rcpp::sugar::Minus_Primitive_Vector<RTYPE,_NA_, Rcpp::VectorBase<RTYPE,_NA_,T> >( lhs, rhs ) ;
+	return Rcpp::sugar::Minus_Primitive_Vector<RTYPE,NA,T>( lhs, rhs ) ;
 }
 
 template <int RTYPE,bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
 inline Rcpp::sugar::Minus_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,
@@ -159,8 +304,8 @@
 ) {
 	return Rcpp::sugar::Minus_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 ) ;
 }
 



More information about the Rcpp-commits mailing list