[Rcpp-commits] r1621 - pkg/Rcpp/inst/include/Rcpp/sugar/functions

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jun 19 12:31:42 CEST 2010


Author: romain
Date: 2010-06-19 12:31:42 +0200 (Sat, 19 Jun 2010)
New Revision: 1621

Modified:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmax.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmin.h
Log:
smarter pmin and pmax

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmax.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmax.h	2010-06-19 10:21:27 UTC (rev 1620)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmax.h	2010-06-19 10:31:42 UTC (rev 1621)
@@ -68,17 +68,15 @@
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 
 	pmax_op_Vector_Primitive( STORAGE right_ ) : 
-		right(right_), isna( Rcpp::traits::is_na<RTYPE>(right_) ){}
+		right(right_) {}
 	
 	inline STORAGE operator()( STORAGE left ) const {
-		if( isna ) return right ;
 		if( Rcpp::traits::is_na<RTYPE>(left) ) return left ;
 		return left > right ? left : right ;
 	}	
 		
 private:
 	STORAGE right ;
-	bool isna ;	
 } ;
 
 template <int RTYPE> class pmax_op_Vector_Primitive<RTYPE,false> {
@@ -86,16 +84,14 @@
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 
 	pmax_op_Vector_Primitive( STORAGE right_ ) : 
-		right(right_), isna( Rcpp::traits::is_na<RTYPE>(right_) ){}
+		right(right_){}
 	
 	inline STORAGE operator()( STORAGE left ) const {
-		if( isna ) return right ;
 		return left > right ? left : right ;
 	}	
 		
 private:
 	STORAGE right ;
-	bool isna ;	
 } ;
 
 
@@ -146,17 +142,19 @@
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 	typedef pmax_op_Vector_Primitive<RTYPE,LHS_NA> OPERATOR ;
 	
-	Pmax_Vector_Primitive( const LHS_TYPE& lhs_, STORAGE rhs ) : 
-		lhs(lhs_), op(rhs) {}
+	Pmax_Vector_Primitive( const LHS_TYPE& lhs_, STORAGE rhs_ ) : 
+		lhs(lhs_), op(rhs_), rhs(rhs_), isna( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
 	
 	inline STORAGE operator[]( int i ) const {
-		return op( lhs[i] ) ;
+		return isna ? rhs : op( lhs[i] ) ;
 	}
 	inline int size() const { return lhs.size() ; }
 	         
 private:
 	const LHS_TYPE& lhs ;
 	OPERATOR op ;
+	STORAGE rhs ;
+	bool isna ;
 } ;
 
 

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmin.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmin.h	2010-06-19 10:21:27 UTC (rev 1620)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmin.h	2010-06-19 10:31:42 UTC (rev 1621)
@@ -68,17 +68,15 @@
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 
 	pmin_op_Vector_Primitive( STORAGE right_ ) : 
-		right(right_), isna( Rcpp::traits::is_na<RTYPE>(right_) ){}
+		right(right_) {}
 	
 	inline STORAGE operator()( STORAGE left ) const {
-		if( isna ) return right ;
 		if( Rcpp::traits::is_na<RTYPE>(left) ) return left ;
 		return left < right ? left : right ;
 	}	
 		
 private:
 	STORAGE right ;
-	bool isna ;	
 } ;
 
 template <int RTYPE> class pmin_op_Vector_Primitive<RTYPE,false> {
@@ -86,16 +84,14 @@
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 
 	pmin_op_Vector_Primitive( STORAGE right_ ) : 
-		right(right_), isna( Rcpp::traits::is_na<RTYPE>(right_) ){}
+		right(right_){}
 	
 	inline STORAGE operator()( STORAGE left ) const {
-		if( isna ) return right ;
 		return left < right ? left : right ;
 	}	
 		
 private:
 	STORAGE right ;
-	bool isna ;	
 } ;
 
 
@@ -146,17 +142,19 @@
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 	typedef pmin_op_Vector_Primitive<RTYPE,LHS_NA> OPERATOR ;
 	
-	Pmin_Vector_Primitive( const LHS_TYPE& lhs_, STORAGE rhs ) : 
-		lhs(lhs_), op(rhs) {}
+	Pmin_Vector_Primitive( const LHS_TYPE& lhs_, STORAGE rhs_ ) : 
+		lhs(lhs_), op(rhs_), rhs(rhs_), isna( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
 	
 	inline STORAGE operator[]( int i ) const {
-		return op( lhs[i] ) ;
+		return isna ? rhs : op( lhs[i] ) ;
 	}
 	inline int size() const { return lhs.size() ; }
 	         
 private:
 	const LHS_TYPE& lhs ;
 	OPERATOR op ;
+	STORAGE rhs ;
+	bool isna ;
 } ;
 
 



More information about the Rcpp-commits mailing list