[Rcpp-commits] r1909 - in pkg/Rcpp: . inst inst/include/Rcpp inst/include/Rcpp/sugar/matrix inst/include/Rcpp/vector

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Aug 4 10:27:34 CEST 2010


Author: romain
Date: 2010-08-04 10:27:34 +0200 (Wed, 04 Aug 2010)
New Revision: 1909

Added:
   pkg/Rcpp/inst/include/Rcpp/vector/LazyVector.h
Modified:
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/Vector.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/outer.h
Log:
better impl of outer

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2010-08-04 07:42:14 UTC (rev 1908)
+++ pkg/Rcpp/TODO	2010-08-04 08:27:34 UTC (rev 1909)
@@ -64,13 +64,4 @@
     o   for character vectors: nchar, grepl, sub, gsub
     
     o   Compound operators: ++,--,+=, -=, ...
-        
-    o   lazy evaluation might not be the best thing for the sugar 
-        implementation of outer. For example : 
-        
-        outer( x + y, z , plus<double>() )
-        
-        has to evaluate x[i] + y[i] as many times as the length
-        of z. perhaps a better implementation would be to 
-        first force the evaluation of x + y.
 

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-08-04 07:42:14 UTC (rev 1908)
+++ pkg/Rcpp/inst/ChangeLog	2010-08-04 08:27:34 UTC (rev 1909)
@@ -2,6 +2,10 @@
 
 	* inst/include/Rcpp/sugar/: rework sugar matrix so that operator()(int,int)
 	is always used instead of operator[](int)
+	
+	* inst/include/Rcpp/sugar/matrix/outer.h: new implementation based on 
+	LazyVector, so that the value from the vector expression is only 
+	retrieved once
 
 2010-08-02  Romain Francois <romain at r-enthusiasts.com>
 

Modified: pkg/Rcpp/inst/include/Rcpp/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Vector.h	2010-08-04 07:42:14 UTC (rev 1908)
+++ pkg/Rcpp/inst/include/Rcpp/Vector.h	2010-08-04 08:27:34 UTC (rev 1909)
@@ -56,6 +56,8 @@
 
 #include <Rcpp/vector/string_proxy.h>
 
+#include <Rcpp/vector/LazyVector.h>
+
 }  // Rcpp
 
 #include <Rcpp/vector/swap.h>

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/outer.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/outer.h	2010-08-04 07:42:14 UTC (rev 1908)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/outer.h	2010-08-04 08:27:34 UTC (rev 1909)
@@ -43,6 +43,10 @@
 	
 	typedef Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
 	typedef Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
+	
+	typedef Rcpp::internal::LazyVector<LHS_T> LHS_LAZY ;
+	typedef Rcpp::internal::LazyVector<RHS_T> RHS_LAZY ;
+	
 	typedef typename Rcpp::traits::r_vector_element_converter<RESULT_R_TYPE>::type converter_type ;
 	typedef typename Rcpp::traits::storage_type<RESULT_R_TYPE>::type STORAGE ;
 	
@@ -58,8 +62,10 @@
 	inline int ncol() const { return nc; }
 	         
 private:
-	const LHS_TYPE& lhs ;
-	const RHS_TYPE& rhs ;
+	      
+	LHS_LAZY lhs ;
+	RHS_LAZY rhs ;
+	
 	Function fun ;
 	int nr, nc ;
 } ;

Added: pkg/Rcpp/inst/include/Rcpp/vector/LazyVector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/LazyVector.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/vector/LazyVector.h	2010-08-04 08:27:34 UTC (rev 1909)
@@ -0,0 +1,70 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// LazyVector.h: Rcpp R/C++ interface class library -- lazy vectors
+//
+// 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__vector__LazyVector_h
+#define Rcpp__vector__LazyVector_h
+
+namespace internal{
+	
+template <typename VECTOR>
+class LazyVector{
+	public:
+		typedef typename VECTOR::r_type r_type ;
+		typedef typename Rcpp::traits::storage_type< r_type::value >::type stored_type ;
+	
+		LazyVector( const VECTOR& vec_ ) : vec(vec_), n(vec_.size()), data(n), known(n,false){}
+		
+		inline stored_type operator[]( int i) const {
+			stored_type res ;
+			if( ! known[i] ) {
+				data[i] = res = vec[i] ;
+				known[i] = true ;
+			} else {
+				res = data[i] ;
+			}
+			return res ;
+		}
+		
+	private:
+		const VECTOR& vec ;
+		int n ;
+		mutable std::vector<stored_type> data ;
+		mutable std::vector<bool> known ;
+} ;
+
+template <int RTYPE>
+class LazyVector< Rcpp::Vector<RTYPE> >{
+public:
+	typedef Rcpp::Vector<RTYPE> VECTOR ;
+	typedef typename VECTOR::Proxy Proxy ;
+	
+	LazyVector( const VECTOR& vec_) : vec(vec_){}
+	inline Proxy operator[]( int i) const { return vec[i] ; }
+	
+private:
+	const VECTOR& vec ;
+		
+} ;
+
+	
+} // internal
+
+#endif



More information about the Rcpp-commits mailing list