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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jan 7 12:46:34 CET 2011


Author: romain
Date: 2011-01-07 12:46:34 +0100 (Fri, 07 Jan 2011)
New Revision: 2846

Modified:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h
Log:
make sum faster by avoiding useless tests for NA

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h	2011-01-07 11:32:11 UTC (rev 2845)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h	2011-01-07 11:46:34 UTC (rev 2846)
@@ -30,9 +30,10 @@
 public:
 	typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	typedef typename Rcpp::traits::Extractor< RTYPE, NA, T>::type VEC_EXT ;
+		
+	Sum( const VEC_TYPE& object_ ) : object(object_.get_ref()){}
 	
-	Sum( const VEC_TYPE& object_ ) : object(object_){}
-	
 	STORAGE get() const {
 		STORAGE result = 0 ;
 		int n = object.size() ;
@@ -46,16 +47,38 @@
 		return result ;
 	}         
 private:
-	const VEC_TYPE& object ;
+	const VEC_EXT& object ;
 } ;
+// RTYPE = REALSXP
+template <bool NA, typename T>
+class Sum<REALSXP,NA,T> : public Lazy< double , Sum<REALSXP,NA,T> > {
+public:
+	typedef typename Rcpp::VectorBase<REALSXP,NA,T> VEC_TYPE ;
+	typedef typename Rcpp::traits::Extractor< REALSXP, NA, T>::type VEC_EXT ;
+		
+	Sum( const VEC_TYPE& object_ ) : object(object_.get_ref()){}
+	
+	double get() const {
+		double result = 0 ;
+		int n = object.size() ;
+		for( int i=0; i<n; i++){
+		   result += object[i] ;
+		}
+		return result ;
+	}         
+private:
+	const VEC_EXT& object ;
+} ;
 
+
 template <int RTYPE, typename T>
 class Sum<RTYPE,false,T> : public Lazy< typename Rcpp::traits::storage_type<RTYPE>::type , Sum<RTYPE,false,T> > {
 public:
 	typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ;
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	typedef typename Rcpp::traits::Extractor< RTYPE, false, T>::type VEC_EXT ;
 	
-	Sum( const VEC_TYPE& object_ ) : object(object_){}
+	Sum( const VEC_TYPE& object_ ) : object(object_.get_ref()){}
 	
 	STORAGE get() const {
 		STORAGE result = 0 ;
@@ -66,7 +89,7 @@
 		return result ;
 	}         
 private:
-	const VEC_TYPE& object ;
+	const VEC_EXT& object ;
 } ;
 
 } // sugar



More information about the Rcpp-commits mailing list