[Rcpp-commits] r1705 - in 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:35:00 CEST 2010


Author: romain
Date: 2010-06-24 10:35:00 +0200 (Thu, 24 Jun 2010)
New Revision: 1705

Modified:
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/include/Rcpp/sugar/operators/divides.h
Log:
adjustments for operator/

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2010-06-24 08:30:47 UTC (rev 1704)
+++ pkg/Rcpp/TODO	2010-06-24 08:35:00 UTC (rev 1705)
@@ -45,8 +45,10 @@
         
 Syntactic sugar
 
-    o   duplicated, unique, count, sum, rep, head, tail, sqrt
+    o   duplicated, unique, count, sum, rep, head, tail, sqrt, log, log10, ln
     
+    o	operator% 
+    
     o   for complex vectors: Re, Im, Mod, Arg, Conj
     
     o   min, max with specialization of the binary operators, so that we can do 

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/operators/divides.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/operators/divides.h	2010-06-24 08:30:47 UTC (rev 1704)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/operators/divides.h	2010-06-24 08:35:00 UTC (rev 1705)
@@ -1,6 +1,6 @@
 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
 //
-// .h: Rcpp R/C++ interface class library -- operator/
+// divides.h: Rcpp R/C++ interface class library -- operator-
 //                                                                      
 // Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
 //
@@ -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 divides{
+	template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T >
+	class Divides_Vector_Vector : public Rcpp::VectorBase<RTYPE,true, Divides_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) ) ;
+		
+		Divides_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 divides<RTYPE,false,RHS_NA>{
+	
+	
+	template <int RTYPE, typename LHS_T, bool RHS_NA, typename RHS_T >
+	class Divides_Vector_Vector<RTYPE,false,LHS_T,RHS_NA,RHS_T> : public Rcpp::VectorBase<RTYPE,true, Divides_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);
+		
+		Divides_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 divides<RTYPE,LHS_NA,false>{
+
+	
+	template <int RTYPE, bool LHS_NA, typename LHS_T, typename RHS_T >
+	class Divides_Vector_Vector<RTYPE,LHS_NA,LHS_T,false,RHS_T> : public Rcpp::VectorBase<RTYPE,true, Divides_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);
+		
+		Divides_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 divides<RTYPE,false,false>{
+	
+	
+	template <int RTYPE, typename LHS_T, typename RHS_T >
+	class Divides_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> : public Rcpp::VectorBase<RTYPE,false, Divides_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;
+		
+		Divides_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 Divides_Vector_Primitive : public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Primitive<RTYPE,_NA_,VEC_TYPE> > {
+	
+	
+	template <int RTYPE, bool NA, typename T>
+	class Divides_Vector_Primitive : public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Primitive<RTYPE,NA,T> > {
 	public:
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		typedef divides<RTYPE,_NA_,true> OPERATOR ;
+		typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
+		typedef STORAGE (Divides_Vector_Primitive::*METHOD)(int) const ;
 		
 		Divides_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
-			lhs(lhs_), rhs(rhs_), op() {}
+			lhs(lhs_), rhs(rhs_){
+			
+			m = Rcpp::traits::is_na<RTYPE>(rhs) ? 
+				&Divides_Vector_Primitive::rhs_is_na :
+				&Divides_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 Divides_Primitive_Vector : public Rcpp::VectorBase<RTYPE,true, Divides_Primitive_Vector<RTYPE,_NA_,VEC_TYPE> > {
+
+	template <int RTYPE, typename T>
+	class Divides_Vector_Primitive<RTYPE,false,T> : public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Primitive<RTYPE,false,T> > {
 	public:
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		typedef divides<RTYPE,_NA_,true> OPERATOR ;
+		typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ;
+		typedef STORAGE (Divides_Vector_Primitive::*METHOD)(int) const ;
 		
+		Divides_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
+			lhs(lhs_), rhs(rhs_){
+
+			m = Rcpp::traits::is_na<RTYPE>(rhs) ? 
+				&Divides_Vector_Primitive::rhs_is_na :
+				&Divides_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 Divides_Primitive_Vector : public Rcpp::VectorBase<RTYPE,true, Divides_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 (Divides_Primitive_Vector::*METHOD)(int) const ;
+		
 		Divides_Primitive_Vector( STORAGE lhs_, const VEC_TYPE& rhs_ ) : 
-			lhs(lhs_), rhs(rhs_), op() {}
+			lhs(lhs_), rhs(rhs_) {
+				
+			m = Rcpp::traits::is_na<RTYPE>(lhs) ? 
+				&Divides_Primitive_Vector::lhs_is_na :
+				&Divides_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 Divides_Vector_Vector : public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Vector<RTYPE,LHS_NA,LHS_VEC_TYPE,RHS_NA,RHS_VEC_TYPE> > {
+	
+	template <int RTYPE, typename T>                                                   
+	class Divides_Primitive_Vector<RTYPE,false,T> : public Rcpp::VectorBase<RTYPE,true, Divides_Primitive_Vector<RTYPE,false,T> > {
 	public:
-		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		typedef divides<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 (Divides_Primitive_Vector::*METHOD)(int) const ;
 		
-		Divides_Vector_Vector( const LHS_VEC_TYPE& lhs_, const RHS_VEC_TYPE& rhs_ ) : 
-			lhs(lhs_), rhs(rhs_), op() {}
+		Divides_Primitive_Vector( STORAGE lhs_, const VEC_TYPE& rhs_ ) : 
+			lhs(lhs_), rhs(rhs_) {
+				
+			m = Rcpp::traits::is_na<RTYPE>(rhs) ? 
+				&Divides_Primitive_Vector::lhs_is_na :
+				&Divides_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::Divides_Vector_Primitive< RTYPE , _NA_ , Rcpp::VectorBase<RTYPE,_NA_,T> >
+template <int RTYPE,bool NA, typename T>
+inline Rcpp::sugar::Divides_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::Divides_Vector_Primitive<RTYPE,_NA_, Rcpp::VectorBase<RTYPE,_NA_,T> >( lhs, rhs ) ;
+	return Rcpp::sugar::Divides_Vector_Primitive<RTYPE,NA,T>( lhs, rhs ) ;
 }
 
 
-template <int RTYPE,bool _NA_, typename T>
-inline Rcpp::sugar::Divides_Primitive_Vector< RTYPE , _NA_ , Rcpp::VectorBase<RTYPE,_NA_,T> >
+template <int RTYPE,bool NA, typename T>
+inline Rcpp::sugar::Divides_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::Divides_Primitive_Vector<RTYPE,_NA_, Rcpp::VectorBase<RTYPE,_NA_,T> >( lhs, rhs ) ;
+	return Rcpp::sugar::Divides_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::Divides_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::Divides_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