[Rcpp-commits] r2162 - in pkg/Rcpp/inst: . examples/ConvolveBenchmarks include/Rcpp/sugar include/Rcpp/sugar/nona include/Rcpp/vector

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Sep 24 14:36:56 CEST 2010


Author: romain
Date: 2010-09-24 14:36:55 +0200 (Fri, 24 Sep 2010)
New Revision: 2162

Added:
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve11_cpp.cpp
   pkg/Rcpp/inst/include/Rcpp/sugar/nona/
   pkg/Rcpp/inst/include/Rcpp/sugar/nona/nona.h
Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
   pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
   pkg/Rcpp/inst/include/Rcpp/vector/RangeIndexer.h
Log:
new sugar function : nona

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-09-24 11:23:10 UTC (rev 2161)
+++ pkg/Rcpp/inst/ChangeLog	2010-09-24 12:36:55 UTC (rev 2162)
@@ -7,6 +7,11 @@
     
     * inst/include/Rcpp/sugar/operators/times.h: speed improvements. Not using 
     pointer to member functions seems to be beneficial.
+    
+    * inst/include/Rcpp/sugar/nona/nona.h: sugar function that wraps a sugar 
+    expression in an object that pretends it does not contain any missing 
+    values. This allows many sugar functions and operators to use their 
+    fast versions when we know that the object does not contain missing values.
 
 2010-09-22  Romain Francois <romain at r-enthusiasts.com>
 

Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh	2010-09-24 11:23:10 UTC (rev 2161)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh	2010-09-24 12:36:55 UTC (rev 2162)
@@ -17,6 +17,7 @@
 R CMD SHLIB convolve8_cpp.cpp
 R CMD SHLIB convolve9_cpp.cpp
 R CMD SHLIB convolve10_cpp.cpp
+R CMD SHLIB convolve11_cpp.cpp
 
 # call R so that we get an interactive session
 Rscript exampleRCode.r

Added: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve11_cpp.cpp
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve11_cpp.cpp	                        (rev 0)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve11_cpp.cpp	2010-09-24 12:36:55 UTC (rev 2162)
@@ -0,0 +1,23 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+
+// This is a rewrite of the 'Writing R Extensions' section 5.10.1 example
+
+#include <Rcpp.h>
+using namespace Rcpp ;
+
+
+RcppExport SEXP convolve11cpp(SEXP a, SEXP b) {
+    NumericVector xa(a); int n_xa = xa.size() ;
+    NumericVector xb(b); int n_xb = xb.size() ;
+    NumericVector xab(n_xa + n_xb - 1,0.0);
+    
+    Range r( 0, n_xb-1 );
+    for(int i=0; i<n_xa; i++, r++){
+    	xab[ r ] += xa[i] * nona(xb) ;
+    }
+    return xab ;
+}
+
+#include "loopmacro.h"
+LOOPMACRO_CPP(convolve11cpp)
+

Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r	2010-09-24 11:23:10 UTC (rev 2161)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r	2010-09-24 12:36:55 UTC (rev 2162)
@@ -16,6 +16,7 @@
 dyn.load("convolve8_cpp.so")
 dyn.load("convolve9_cpp.so")
 dyn.load("convolve10_cpp.so")
+dyn.load("convolve11_cpp.so")
 
 ## now run each one once for comparison of results,
 ## and define test functions
@@ -26,6 +27,7 @@
 Rcpp_New_std_inside <- function(n,a,b) .Call("convolve3cpp__loop", n, a, b, PACKAGE = "Rcpp" )
 Rcpp_New_ptr <- function(n,a,b) .Call("convolve4cpp__loop", n, a, b)
 Rcpp_New_sugar <- function(n,a,b) .Call("convolve5cpp__loop", n, a, b)
+Rcpp_New_sugar_nona <- function(n,a,b) .Call("convolve11cpp__loop", n, a, b)
 R_API_naive <- function(n,a,b) .Call("convolve7__loop", n, a, b)
 Rcpp_New_std_2 <- function(n,a,b) .Call("convolve8cpp__loop", n, a, b)
 Rcpp_New_std_3 <- function(n,a,b) .Call("convolve9cpp__loop", n, a, b)
@@ -55,6 +57,7 @@
                 Rcpp_New_std_inside(REPS,a,b),
                 Rcpp_New_ptr(REPS,a,b),
                 Rcpp_New_sugar(REPS,a,b),
+                Rcpp_New_sugar_nona(REPS,a,b),
                 Rcpp_New_std_2(REPS,a,b),
                 Rcpp_New_std_3(REPS,a,b),
                 Rcpp_New_std_4(REPS,a,b),

Added: pkg/Rcpp/inst/include/Rcpp/sugar/nona/nona.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/nona/nona.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/nona/nona.h	2010-09-24 12:36:55 UTC (rev 2162)
@@ -0,0 +1,70 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// nona.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef RCPP_SUGAR_NONA_NONA_H
+#define RCPP_SUGAR_NONA_NONA_H
+
+namespace Rcpp{
+namespace sugar {     
+
+    template <int RTYPE, bool NA, typename VECTOR>
+    class Nona : public Rcpp::VectorBase<RTYPE,false, Nona<RTYPE,NA,VECTOR> > {
+    public:
+        typedef typename Rcpp::VectorBase<RTYPE,NA,VECTOR> SUGAR_TYPE ;
+        typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	
+        Nona( const SUGAR_TYPE& expr) : data(expr.get_ref()){} 
+        
+        inline int size() const { return data.size() ; }
+        inline STORAGE operator[](int i) const { return data[i] ; }
+        
+    private:
+        const VECTOR& data ;    
+    } ;
+    
+    // specialization when the expression is actually a vector expression
+    template <int RTYPE, bool NA>
+    class Nona< RTYPE,NA,Rcpp::Vector<RTYPE> > : public Rcpp::VectorBase<RTYPE,false, Nona<RTYPE,NA,Rcpp::Vector<RTYPE> > > {
+    public:
+        typedef typename Rcpp::VectorBase<RTYPE,NA, Rcpp::Vector<RTYPE> > SUGAR_TYPE ;
+        typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+        typedef typename Rcpp::Vector<RTYPE>::iterator iterator ;
+	
+        Nona( const SUGAR_TYPE& expr) : data(expr.get_ref().begin()){} 
+        
+        inline int size() const { return data.size() ; }
+        inline STORAGE operator[](int i) const { return data[i] ; }
+        
+    private:
+        iterator data ;    
+    } ;
+    
+    
+}
+
+template <int RTYPE, bool NA, typename VECTOR>
+inline sugar::Nona<RTYPE,NA,VECTOR> nona( const Rcpp::VectorBase<RTYPE,NA,VECTOR>& vec ){
+    return sugar::Nona<RTYPE,NA,VECTOR>( vec ) ;
+}
+
+}
+
+#endif

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h	2010-09-24 11:23:10 UTC (rev 2161)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h	2010-09-24 12:36:55 UTC (rev 2162)
@@ -24,6 +24,8 @@
 
 #include <Rcpp/sugar/block/block.h>
 
+#include <Rcpp/sugar/nona/nona.h>
+
 #include <Rcpp/sugar/operators/operators.h>
 #include <Rcpp/sugar/functions/functions.h>
 #include <Rcpp/sugar/matrix/matrix_functions.h>

Modified: pkg/Rcpp/inst/include/Rcpp/vector/RangeIndexer.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/RangeIndexer.h	2010-09-24 11:23:10 UTC (rev 2161)
+++ pkg/Rcpp/inst/include/Rcpp/vector/RangeIndexer.h	2010-09-24 12:36:55 UTC (rev 2162)
@@ -55,24 +55,27 @@
 	
 	template <bool NA, typename T>	
 	RangeIndexer& operator*=( const Rcpp::VectorBase<RTYPE,NA,T>& x){
-		for( int i=0; i<size_; i++){
-			start[i] *= x[i] ;
+		const T& input = x.get_ref() ;
+	    for( int i=0; i<size_; i++){
+			start[i] *= input[i] ;
 		}
 		return *this ;
 	}
 	
 	template <bool NA, typename T>	
 	RangeIndexer& operator-=( const Rcpp::VectorBase<RTYPE,NA,T>& x){
-		for( int i=0; i<size_; i++){
-			start[i] -= x[i] ;
+		const T& input = x.get_ref() ;
+	    for( int i=0; i<size_; i++){
+			start[i] -= input[i] ;
 		}
 		return *this ;
 	}
 	
 	template <bool NA, typename T>	
 	RangeIndexer& operator/=( const Rcpp::VectorBase<RTYPE,NA,T>& x){
-		for( int i=0; i<size_; i++){
-			start[i] /= x[i] ;
+		const T& input = x.get_ref() ;
+	    for( int i=0; i<size_; i++){
+			start[i] /= input[i] ;
 		}
 		return *this ;
 	}



More information about the Rcpp-commits mailing list