[Rcpp-commits] r1619 - in pkg/Rcpp: . inst/include/Rcpp/sugar/functions inst/unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jun 19 12:14:43 CEST 2010


Author: romain
Date: 2010-06-19 12:14:43 +0200 (Sat, 19 Jun 2010)
New Revision: 1619

Modified:
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmin.h
   pkg/Rcpp/inst/unitTests/runit.sugar.pmin.R
Log:
Vector/Primitive and Primitive/Vector version of pmin

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2010-06-19 09:56:44 UTC (rev 1618)
+++ pkg/Rcpp/TODO	2010-06-19 10:14:43 UTC (rev 1619)
@@ -45,7 +45,7 @@
 	
 Syntactic sugar
 
-    o   duplicated, unique, count, sum, rep, head, tail, pmin, pmax
+    o   duplicated, unique, count, sum, rep, head, tail, pmax
     
     o	min, max with specialization of the binary operators, so that we can do 
     	things like this lazily: 

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmin.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmin.h	2010-06-19 09:56:44 UTC (rev 1618)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmin.h	2010-06-19 10:14:43 UTC (rev 1619)
@@ -37,6 +37,7 @@
 	
 } ;
 template <int RTYPE, bool LHS_NA> class pmin_op<RTYPE,LHS_NA,false> {
+public:
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 	
 	inline STORAGE operator()( STORAGE left, STORAGE right ) const {
@@ -45,6 +46,7 @@
 	}
 } ;
 template <int RTYPE, bool RHS_NA> class pmin_op<RTYPE,false,RHS_NA> {
+public:
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 	
 	inline STORAGE operator()( STORAGE left, STORAGE right ) const {
@@ -53,6 +55,7 @@
 	}
 } ;
 template <int RTYPE> class pmin_op<RTYPE,false,false> {
+public:
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 	
 	inline STORAGE operator()( STORAGE left, STORAGE right ) const {
@@ -60,7 +63,43 @@
 	}
 } ;
 
+template <int RTYPE,bool NA> class pmin_op_Vector_Primitive {
+public:
+	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+
+	pmin_op_Vector_Primitive( STORAGE right_ ) : 
+		right(right_), isna( Rcpp::traits::is_na<RTYPE>(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> {
+public:
+	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+
+	pmin_op_Vector_Primitive( STORAGE right_ ) : 
+		right(right_), isna( Rcpp::traits::is_na<RTYPE>(right_) ){}
+	
+	inline STORAGE operator()( STORAGE left ) const {
+		if( isna ) return right ;
+		return left < right ? left : right ;
+	}	
+		
+private:
+	STORAGE right ;
+	bool isna ;	
+} ;
+
+
+
 template <
 	int RTYPE, 
 	bool LHS_NA, typename LHS_T, 
@@ -90,7 +129,38 @@
 	const RHS_TYPE& rhs ;
 	OPERATOR op ;
 } ;
+
+
+
+template <
+	int RTYPE, 
+	bool LHS_NA, typename LHS_T
+	>
+class Pmin_Vector_Primitive : public VectorBase< 
+	RTYPE , 
+	true ,
+	Pmin_Vector_Primitive<RTYPE,LHS_NA,LHS_T>
+> {
+public:
+	typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
+	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) {}
+	
+	inline STORAGE operator[]( int i ) const {
+		return op( lhs[i] ) ;
+	}
+	inline int size() const { return lhs.size() ; }
+	         
+private:
+	const LHS_TYPE& lhs ;
+	OPERATOR op ;
+} ;
+
+
+
 } // sugar
 
 template <
@@ -106,6 +176,32 @@
 	return sugar::Pmin_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>( lhs, rhs ) ;
 }
 
+template <
+	int RTYPE, 
+	bool LHS_NA, typename LHS_T
+>
+inline sugar::Pmin_Vector_Primitive<RTYPE,LHS_NA,LHS_T> 
+pmin( 
+	const Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>& lhs, 
+	typename Rcpp::traits::storage_type<RTYPE>::type rhs 
+	){
+	return sugar::Pmin_Vector_Primitive<RTYPE,LHS_NA,LHS_T>( lhs, rhs ) ;
+}
+
+
+template <
+	int RTYPE, 
+	bool RHS_NA, typename RHS_T
+>
+inline sugar::Pmin_Vector_Primitive<RTYPE,RHS_NA,RHS_T> 
+pmin( 
+	typename Rcpp::traits::storage_type<RTYPE>::type lhs,  
+	const Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>& rhs 
+	){
+	return sugar::Pmin_Vector_Primitive<RTYPE,RHS_NA,RHS_T>( rhs, lhs ) ;
+}
+
+
 } // Rcpp
 
 #endif

Modified: pkg/Rcpp/inst/unitTests/runit.sugar.pmin.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.pmin.R	2010-06-19 09:56:44 UTC (rev 1618)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.pmin.R	2010-06-19 10:14:43 UTC (rev 1619)
@@ -31,3 +31,21 @@
 	checkEquals( fx(1:10, 10:1) , c(1:5,5:1) )
 }
 
+test.sugar.pmin.one <- function( ){
+
+	fx <- cxxfunction( signature( x = "numeric" ), '
+		NumericVector xx(x) ;
+		return List::create( 
+			pmin( xx, 5), 
+			pmin( 5, xx)
+			) ;
+	', plugin = "Rcpp" )
+	
+	checkEquals( fx(1:10) , 
+		list( 
+			c(1:5,rep(5,5)), 
+			c(1:5,rep(5,5))
+			) )
+}
+
+



More information about the Rcpp-commits mailing list