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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Feb 17 17:10:48 CET 2010


Author: romain
Date: 2010-02-17 17:10:48 +0100 (Wed, 17 Feb 2010)
New Revision: 727

Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/unitTests/runit.GenericVector.R
   pkg/Rcpp/src/Rcpp/SEXP_Vector.h
   pkg/Rcpp/src/SEXP_Vector.cpp
Log:
improved SEXP_Vector::operator[](string)

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-02-17 15:28:08 UTC (rev 726)
+++ pkg/Rcpp/inst/ChangeLog	2010-02-17 16:10:48 UTC (rev 727)
@@ -1,5 +1,12 @@
 2010-02-17  Romain Francois <romain at r-enthusiasts.com>
 
+	* src/Rcpp/SEXP_Vector.h : The lhs use of operator[](string) has 
+	been improved so that if the name does not exist in the vector, 
+	the vector grows.
+	
+	* inst/unitTests/runit.GenericVector.R: new unit test
+	test.List.implicit.push.back about the new operator[](string)
+
 	* src/Rcpp/SimpleVector.h : new template class SimpleMatrix<int>
 	that derives from SimpleVector<int>, with typedefs NumericMatrix
 	IntegerMatrix, LogicalMatrix, RawMatrix, ComplexMatrix. The actual 

Modified: pkg/Rcpp/inst/unitTests/runit.GenericVector.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.GenericVector.R	2010-02-17 15:28:08 UTC (rev 726)
+++ pkg/Rcpp/inst/unitTests/runit.GenericVector.R	2010-02-17 16:10:48 UTC (rev 727)
@@ -231,6 +231,16 @@
 		msg = "List.erase (range version)" )
 }
 
+test.List.implicit.push.back <- function(){
 
+        funx <- cfunction( signature(),
+        '
+        List list ;
+        list["foo"] = 10 ;
+        list["bar" ] = "foobar" ;
+        return list ;
+        ', Rcpp = TRUE, includes = "using namespace Rcpp;" )
+        checkEquals( funx(), list( foo = 10, bar = "foobar" ), msg = "List implicit push back" )
+}
 
 

Modified: pkg/Rcpp/src/Rcpp/SEXP_Vector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/SEXP_Vector.h	2010-02-17 15:28:08 UTC (rev 726)
+++ pkg/Rcpp/src/Rcpp/SEXP_Vector.h	2010-02-17 16:10:48 UTC (rev 727)
@@ -33,6 +33,7 @@
 public:
 	
 	class iterator ;
+	class NameProxy ;
 	
 	class Proxy {
 	public:
@@ -63,7 +64,7 @@
 		void set(SEXP x) ;
 		SEXP get() const ;
 	} ;
-
+	
 	class iterator {
 	public:
 		typedef Proxy& reference ;
@@ -113,9 +114,6 @@
 		return Proxy(*this, offset(i) ) ; 
 	}
 	
-	const Proxy operator[]( const std::string& name) const ; 
-	Proxy operator[](const std::string& name) ;
-    	
 	inline iterator begin() { return iterator(*this, 0) ; }
 	inline iterator end() { return iterator(*this, size() ) ; }
 	
@@ -130,6 +128,52 @@
 template <int RTYPE>
 class SEXP_Vector : public SEXP_Vector_Base{
 public:
+	
+	class NameProxy {
+	public:
+		NameProxy( SEXP_Vector& v, const std::string& name_ ) : parent(v), name(name_){} ;
+		
+		NameProxy& operator=(SEXP rhs) { 
+			set( rhs) ; 
+			return *this ;
+		}
+		
+		NameProxy& operator=(const NameProxy& rhs){
+			set( rhs.get() ) ;
+			return *this;
+		}
+		
+		template <typename T>
+		NameProxy& operator=( const T& rhs){
+			set( wrap(rhs) ) ;
+			return *this; 
+		}
+		
+		inline operator SEXP() const { 
+			return get() ;
+		}
+		template <typename U> operator U(){
+			return as<U>( get() ) ;
+		}
+		
+	private:
+		SEXP_Vector & parent; 
+		std::string name ;
+		void set(SEXP x) {
+			int index = 0 ;
+			try{
+				index = parent.offset(name) ;
+				// parent[ index ] = x ;
+			} catch( const RObject::index_out_of_bounds& ex ){
+				parent.push_back( Named(name, x ) ); 
+			}
+		}
+		
+		SEXP get() const {
+			return (SEXP) parent[ parent.offset(name) ] ;
+		}
+	} ;
+	
 	SEXP_Vector() : SEXP_Vector_Base(){} ; 
 	
 	SEXP_Vector(const SEXP_Vector& other) : SEXP_Vector_Base(){
@@ -166,6 +210,13 @@
 	} ;
 #endif
 	
+	const NameProxy operator[]( const std::string& name) const {
+		return NameProxy( *this, name ) ;
+	}
+	NameProxy operator[](const std::string& name) {
+		return NameProxy( const_cast<SEXP_Vector&>(*this), name ) ;
+	}
+    	
 	template <typename InputIterator>
 	void assign( InputIterator first, InputIterator last){
 		/* FIXME: we might not need the wrap if the object already 

Modified: pkg/Rcpp/src/SEXP_Vector.cpp
===================================================================
--- pkg/Rcpp/src/SEXP_Vector.cpp	2010-02-17 15:28:08 UTC (rev 726)
+++ pkg/Rcpp/src/SEXP_Vector.cpp	2010-02-17 16:10:48 UTC (rev 727)
@@ -72,14 +72,6 @@
 	return Proxy(*this, offset(i,j) ) ;
 }
 
-const SEXP_Vector_Base::Proxy SEXP_Vector_Base::operator[]( const std::string& name) const {
-	return Proxy( const_cast<SEXP_Vector_Base&>(*this), offset(name) ) ;
-}
-SEXP_Vector_Base::Proxy SEXP_Vector_Base::operator[](const std::string& name) {
-	return Proxy(*this, offset(name) ) ;
-}
-    	
-
 } // namespace Rcpp
 
 namespace std{



More information about the Rcpp-commits mailing list