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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jan 29 10:39:28 CET 2012


Author: romain
Date: 2012-01-29 10:39:22 +0100 (Sun, 29 Jan 2012)
New Revision: 3448

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/diff.h
Log:
custom version of diff for the REALSXP case, skipping some unnecessary NA tests

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-01-28 02:29:06 UTC (rev 3447)
+++ pkg/Rcpp/ChangeLog	2012-01-29 09:39:22 UTC (rev 3448)
@@ -1,3 +1,8 @@
+2012-01-29  Romain Francois  <romain at r-enthusiasts.com>
+
+        * inst/include/Rcpp/sugar/diff.h: custom implementation of diff for the
+        REALSXP case, skipping unnecessary NA tests
+
 2012-01-24  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/doc/Rcpp-FAQ/Rcpp-FAQ.Rnw: New FAQ entry on passing a single

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/diff.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/diff.h	2012-01-28 02:29:06 UTC (rev 3447)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/diff.h	2012-01-29 09:39:22 UTC (rev 3448)
@@ -63,6 +63,28 @@
 	mutable bool was_na ;
 } ;
 
+template <typename LHS_T, bool LHS_NA>
+class Diff<REALSXP, LHS_NA, LHS_T> : public Rcpp::VectorBase< REALSXP, LHS_NA, Diff<REALSXP,LHS_NA,LHS_T> >{
+public:
+	typedef typename Rcpp::VectorBase<REALSXP,LHS_NA,LHS_T> LHS_TYPE ;
+	
+	Diff( const LHS_TYPE& lhs_ ) : lhs(lhs_), previous(lhs_[0]) {}
+	
+	inline double operator[]( int i ) const {
+		double y = lhs[i+1] ;
+		double res = y - previous ;
+		previous = y ;
+		return res ;
+	}
+	inline int size() const { return lhs.size() - 1 ; }
+	         
+private:
+	const LHS_TYPE& lhs ;
+	mutable double previous ;
+} ;
+
+
+
 template <int RTYPE, typename LHS_T>
 class Diff<RTYPE,false,LHS_T> : public Rcpp::VectorBase< RTYPE, false , Diff<RTYPE,false,LHS_T> > {
 public:



More information about the Rcpp-commits mailing list