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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 17 22:16:50 CEST 2010


Author: romain
Date: 2010-06-17 22:16:49 +0200 (Thu, 17 Jun 2010)
New Revision: 1581

Modified:
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/include/Rcpp/sugar/plus.h
   pkg/Rcpp/inst/unitTests/runit.sugar.plus.R
Log:
 Vector + Vector

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2010-06-17 19:53:58 UTC (rev 1580)
+++ pkg/Rcpp/TODO	2010-06-17 20:16:49 UTC (rev 1581)
@@ -45,7 +45,7 @@
     
     o	for matrices: row, col, lower_tri, upper_tri
 	
-    o   other operators: +,-, ...
+    o   other operators: -, /, *, ...
     
     o	lazy version of sapply as in this thread:
     	http://article.gmane.org/gmane.comp.lang.r.rcpp/455

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/plus.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/plus.h	2010-06-17 19:53:58 UTC (rev 1580)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/plus.h	2010-06-17 20:16:49 UTC (rev 1581)
@@ -81,6 +81,27 @@
 		STORAGE rhs ;
 		OPERATOR op ; 
 	} ;
+	
+	template <int RTYPE, bool LHS_NA, typename LHS_VEC_TYPE, bool RHS_NA, typename RHS_VEC_TYPE >
+	class Plus_Vector_Vector : public Rcpp::VectorBase<RTYPE,true, Plus_Vector_Vector<RTYPE,LHS_NA,LHS_VEC_TYPE,RHS_NA,RHS_VEC_TYPE> > {
+	public:
+		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+		typedef plus<RTYPE,LHS_NA,RHS_NA> OPERATOR ;
+		
+		Plus_Vector_Vector( const LHS_VEC_TYPE& lhs_, const RHS_VEC_TYPE& rhs_ ) : 
+			lhs(lhs_), rhs(rhs_), op() {}
+		
+		inline STORAGE operator[]( int i ) const {
+			return op.apply( lhs[i], rhs[i] ) ;
+		}
+		
+		inline int size() const { return lhs.size() ; }
+		
+	private:
+		const LHS_VEC_TYPE& lhs ;
+		const RHS_VEC_TYPE& rhs ;
+		OPERATOR op ; 
+	} ;
 }
 }
 
@@ -103,4 +124,21 @@
 	return Rcpp::sugar::Plus_Vector_Primitive<RTYPE,_NA_, Rcpp::VectorBase<RTYPE,_NA_,T> >( lhs, rhs ) ;
 }
 
+template <int RTYPE,bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
+inline Rcpp::sugar::Plus_Vector_Vector< 
+	RTYPE , 
+	LHS_NA, Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>, 
+	RHS_NA, Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>
+	>
+operator+( 
+	const Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>& lhs,
+	const Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>& rhs
+) {
+	return Rcpp::sugar::Plus_Vector_Vector<
+		RTYPE, 
+		LHS_NA, Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>,
+		RHS_NA, Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>
+		>( lhs, rhs ) ;
+}
+
 #endif

Modified: pkg/Rcpp/inst/unitTests/runit.sugar.plus.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.plus.R	2010-06-17 19:53:58 UTC (rev 1580)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.plus.R	2010-06-17 20:16:49 UTC (rev 1581)
@@ -23,11 +23,12 @@
 		IntegerVector xx(x) ;
 		return List::create(
 			xx + 10, 
-			10 + xx 
+			10 + xx, 
+			xx + xx
 			) ;
 	', plugin = "Rcpp" )
 	
-	checkEquals( fx(1:10) , list( 11:20,11:20)  )
+	checkEquals( fx(1:10) , list( 11:20,11:20,1:10+1:10)  )
 }
 
 test.sugar.plus.seqlen <- function( ){
@@ -35,10 +36,11 @@
 	fx <- cxxfunction( signature(), '
 		return List::create(
 			seq_len(10) + 10, 
-			10 + seq_len(10) 
+			10 + seq_len(10),
+			seq_len(10) + seq_len(10)
 			) ;
 	', plugin = "Rcpp" )
 	
-	checkEquals( fx() , list( 11:20,11:20)  )
+	checkEquals( fx() , list( 11:20,11:20, 1:10+1:10)  )
 }
 



More information about the Rcpp-commits mailing list