[Rcpp-commits] r295 - in pkg: inst/unitTests src src/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jan 7 13:51:29 CET 2010


Author: romain
Date: 2010-01-07 13:51:29 +0100 (Thu, 07 Jan 2010)
New Revision: 295

Modified:
   pkg/inst/unitTests/runit.GenericVector.R
   pkg/src/CharacterVector.cpp
   pkg/src/GenericVector.cpp
   pkg/src/Rcpp/GenericVector.h
Log:
implement the proxy pattern for GenericVector to get the comfort of operator[]

Modified: pkg/inst/unitTests/runit.GenericVector.R
===================================================================
--- pkg/inst/unitTests/runit.GenericVector.R	2010-01-07 12:26:02 UTC (rev 294)
+++ pkg/inst/unitTests/runit.GenericVector.R	2010-01-07 12:51:29 UTC (rev 295)
@@ -24,7 +24,7 @@
 test.List <- function(){
 	funx <- cfunction(signature(), '
 	List x(10) ;
-	for( int i=0; i<10; i++) x.set( i, Rf_ScalarInteger( i * 2) )  ;
+	for( int i=0; i<10; i++) x[i] = Rf_ScalarInteger( i * 2)  ;
 	return x ;', 
 		Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
 	checkEquals( funx(), as.list( 2*0:9), msg = "GenericVector" )

Modified: pkg/src/CharacterVector.cpp
===================================================================
--- pkg/src/CharacterVector.cpp	2010-01-07 12:26:02 UTC (rev 294)
+++ pkg/src/CharacterVector.cpp	2010-01-07 12:51:29 UTC (rev 295)
@@ -75,6 +75,8 @@
 	return RCPP_VECTOR_PTR(m_sexp) + LENGTH(m_sexp) ;
 }
 
+/* proxy stuff */
+
 CharacterVector::StringProxy::StringProxy(CharacterVector& v, int i) :
 	parent(v), index(i){}
 

Modified: pkg/src/GenericVector.cpp
===================================================================
--- pkg/src/GenericVector.cpp	2010-01-07 12:26:02 UTC (rev 294)
+++ pkg/src/GenericVector.cpp	2010-01-07 12:51:29 UTC (rev 295)
@@ -68,10 +68,30 @@
 	return RCPP_VECTOR_PTR(m_sexp) + LENGTH(m_sexp) ;
 }
 
-/* does this work for = ? */
-SEXP& GenericVector::operator[](int i) const {
-	return *(RCPP_VECTOR_PTR(m_sexp) + i) ;
+/* proxy stuff */
+
+GenericVector::Proxy::Proxy(GenericVector& v, int i) :
+	parent(v), index(i){}
+
+GenericVector::Proxy::operator SEXP() const{
+	return VECTOR_ELT( parent, index ) ;
 }
 
+GenericVector::Proxy& GenericVector::Proxy::operator=( const Proxy& rhs){
+	SET_VECTOR_ELT( parent, index, VECTOR_ELT( rhs.parent, rhs.index) ) ;
+}
 
+GenericVector::Proxy& GenericVector::Proxy::operator=( SEXP rhs){
+	SET_VECTOR_ELT( parent, index, rhs ) ;
+}
+
+const GenericVector::Proxy GenericVector::operator[](int i) const {
+	return Proxy(const_cast<GenericVector&>(*this), i) ;
+}
+
+GenericVector::Proxy GenericVector::operator[](int i) {
+	return Proxy(*this, i ) ;
+}
+
+
 } // namespace 

Modified: pkg/src/Rcpp/GenericVector.h
===================================================================
--- pkg/src/Rcpp/GenericVector.h	2010-01-07 12:26:02 UTC (rev 294)
+++ pkg/src/Rcpp/GenericVector.h	2010-01-07 12:51:29 UTC (rev 295)
@@ -35,6 +35,24 @@
 class GenericVector : public RObject {     
 public:
 
+	/* much inspired from item 30 of more effective C++ */
+	class Proxy {
+	public:
+		Proxy( GenericVector& v, int index ) ;
+		
+		/* lvalue uses */
+		Proxy& operator=(const Proxy& rhs) ;
+		Proxy& operator=(SEXP rhs) ;
+		
+		/* rvalue use */
+		operator SEXP() const ;
+		
+	private:
+		GenericVector& parent; 
+		int index ;
+	} ;
+
+
 	GenericVector(SEXP x) throw(not_compatible);
 	GenericVector( int size) ;
 	
@@ -58,8 +76,11 @@
 	SEXP* begin(); 
 	SEXP* end() ;
 	
-	SEXP& operator[]( int i ) const ;
+	const Proxy operator[]( int i ) const ;
+	Proxy operator[]( int i ) ;
 	
+	friend class Proxy; 
+	
 } ;
 
 typedef GenericVector List ;



More information about the Rcpp-commits mailing list