[Rcpp-commits] r510 - in pkg: inst src src/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jan 28 20:13:42 CET 2010


Author: romain
Date: 2010-01-28 20:13:41 +0100 (Thu, 28 Jan 2010)
New Revision: 510

Modified:
   pkg/inst/ChangeLog
   pkg/src/DottedPair.cpp
   pkg/src/Rcpp/DottedPair.h
Log:
DottedPair::Proxy no more using lazy evaluation

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-28 18:46:01 UTC (rev 509)
+++ pkg/inst/ChangeLog	2010-01-28 19:13:41 UTC (rev 510)
@@ -1,5 +1,10 @@
 2010-01-28  Romain Francois <francoisromain at free.fr>
 
+	* src/Rcpp/DottedPair.h: DottedPair::Proxy are no more lazy, i.e
+	traversal of the pair list happens at construction time, so
+	that the proxy can be created once and used many times more
+	efficiently.
+
 	* src/Rcpp/DottedPair.h: Pairlist and Language are now derived
 	from the new virtual class DottedPair since both class were 
 	almost identical

Modified: pkg/src/DottedPair.cpp
===================================================================
--- pkg/src/DottedPair.cpp	2010-01-28 18:46:01 UTC (rev 509)
+++ pkg/src/DottedPair.cpp	2010-01-28 19:13:41 UTC (rev 510)
@@ -38,58 +38,36 @@
 		}
 	}
 	
-	DottedPair::Proxy::Proxy( DottedPair& v, const size_t& index_ ) : 
-		parent(v), index(index_){}
-	
-	DottedPair::Proxy& DottedPair::Proxy::operator=(const Proxy& rhs){
-		if( index < 0 || index >= parent.length() ) throw index_out_of_bounds() ;
+	DottedPair::Proxy::Proxy( DottedPair& v, const size_t& index_ ) throw(index_out_of_bounds) : node(){
+		if( index_ >= v.length() ) throw index_out_of_bounds() ;
+		SEXP x = v ; /* implicit conversion */
 		size_t i = 0 ;
-		SEXP x = parent.asSexp() ; 
-		while( i < index ) {
+		while( i<index_) {
 			x = CDR(x) ;
-			i++ ;
+			++i ;
 		}
+		node = x ;
+	}
+	
+	DottedPair::Proxy& DottedPair::Proxy::operator=(const Proxy& rhs){
 		SEXP y = rhs ; /* implicit conversion */
-		SETCAR( x, y ) ;
-		// if( index != 0 ) SET_TAG( x, Rf_install( rhs.getTag() ) ) ;
+		SETCAR( node, y ) ;
 		return *this ;
 	}
 	
 	DottedPair::Proxy& DottedPair::Proxy::operator=(SEXP rhs){
-		if( index < 0 || index >= parent.length() ) throw index_out_of_bounds() ;
-		SEXP x = parent.asSexp() ; 
-		size_t i = 0 ;
-		while( i < index ) {
-			x = CDR(x) ;
-			i++ ;
-		}
-		SETCAR( x, rhs) ;
+		SETCAR( node, rhs) ;
 		return *this ;
 	}
 	
 	DottedPair::Proxy& DottedPair::Proxy::operator=(const Named& rhs){
-		if( index < 0 || index >= parent.length() ) throw index_out_of_bounds() ;
-		size_t i = 0 ;
-		SEXP x = parent.asSexp() ; 
-		while( i < index ) {
-			x = CDR(x) ;
-			i++ ;
-		}
-		SEXP y = rhs.getSEXP() ;
-		SETCAR( x, y ) ;
-		if( index != 0 ) SET_TAG( x, Symbol( rhs.getTag() ) ) ;
+		SETCAR( node, rhs.getSEXP() ) ;
+		SET_TAG( node, Rf_install( rhs.getTag().c_str() ) ) ;
 		return *this ;
 	}
 		
 	DottedPair::Proxy::operator SEXP() {
-		if( index < 0 || index >= parent.length() ) throw index_out_of_bounds() ;
-		SEXP x = parent.asSexp() ; 
-		size_t i = 0 ;
-		while( i < index ) {
-			x = CDR(x) ;
-			i++ ;
-		}
-		return CAR(x) ;
+		return CAR(node) ;
 	}
 		
 	const DottedPair::Proxy DottedPair::operator[]( int i ) const {

Modified: pkg/src/Rcpp/DottedPair.h
===================================================================
--- pkg/src/Rcpp/DottedPair.h	2010-01-28 18:46:01 UTC (rev 509)
+++ pkg/src/Rcpp/DottedPair.h	2010-01-28 19:13:41 UTC (rev 510)
@@ -139,7 +139,7 @@
 	
 	class Proxy {
 	public:
-		Proxy( DottedPair& v, const size_t& index_ ) ; 
+		Proxy( DottedPair& v, const size_t& index_ ) throw(index_out_of_bounds) ; 
 		
 		/* lvalue uses */
 		Proxy& operator=(const Proxy& rhs) ; 
@@ -147,7 +147,8 @@
 		
 		template <typename T>
 		Proxy& operator=(const T& rhs){
-			parent.replace( index, rhs ) ;
+			SEXP y = wrap(rhs) ;
+			SETCAR( node, y ) ;
 			return *this ;
 		}
 		Proxy& operator=(const Named& rhs) ;
@@ -156,19 +157,11 @@
 		operator SEXP() ;
 		
 		template <typename T> operator T() const {
-			if( index < 0 || index >= parent.length() ) throw index_out_of_bounds() ;
-			SEXP x = parent.asSexp() ; 
-			size_t i = 0 ;
-			while( i < index ) {
-				x = CDR(x) ;
-				i++ ;
-			}
-			return as<T>( CAR(x) ) ;
+			return as<T>( CAR(node) ) ;
 		}
 		
 	private:
-		DottedPair& parent; 
-		size_t index ;
+		RObject node ;
 	} ;
 	
 	const Proxy operator[]( int i ) const ;



More information about the Rcpp-commits mailing list