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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Feb 25 10:26:46 CET 2010


Author: romain
Date: 2010-02-25 10:26:46 +0100 (Thu, 25 Feb 2010)
New Revision: 786

Modified:
   pkg/Rcpp/inst/unitTests/runit.Column.R
   pkg/Rcpp/inst/unitTests/runit.Row.R
   pkg/Rcpp/src/CharacterVector.cpp
   pkg/Rcpp/src/Rcpp/CharacterVector.h
   pkg/Rcpp/src/Rcpp/MatrixColumn.h
   pkg/Rcpp/src/Rcpp/MatrixRow.h
   pkg/Rcpp/src/Rcpp/VectorBase.h
Log:
rename StringProxy -> Proxy to uniformize vector classes

Modified: pkg/Rcpp/inst/unitTests/runit.Column.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Column.R	2010-02-24 18:44:33 UTC (rev 785)
+++ pkg/Rcpp/inst/unitTests/runit.Column.R	2010-02-25 09:26:46 UTC (rev 786)
@@ -27,19 +27,6 @@
 	checkEquals( funx( x ), sum( x[,1] ) , msg = "iterating over a column" )
 }
 
-test.NumericMatrix.column.operator.equal <- function(){
-	funx <- cfunction(signature(x = "matrix" ), '
-		NumericMatrix m(x) ;
-		NumericMatrix::Column first  = m.column(0) ;
-		NumericMatrix::Column second = m.column(1) ;
-		first = second ;
-		return R_NilValue ;
-	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
-	x <- matrix( 1:16 + .5, ncol = 4 )
-	funx( x )
-	checkEquals( x[,1], x[,2], msg = "column(0) = column(1) " )
-}
-
 test.CharacterMatrix.column <- function(){
 	funx <- cfunction(signature(x = "matrix" ), '
 		CharacterVector m(x) ;

Modified: pkg/Rcpp/inst/unitTests/runit.Row.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Row.R	2010-02-24 18:44:33 UTC (rev 785)
+++ pkg/Rcpp/inst/unitTests/runit.Row.R	2010-02-25 09:26:46 UTC (rev 786)
@@ -27,20 +27,6 @@
 	checkEquals( funx( x ), sum( x[1,] ), msg = "iterating over a row" )
 }
 
-test.NumericMatrix.row.operator.equal <- function(){
-	funx <- cfunction(signature(x = "matrix" ), '
-		NumericMatrix m(x) ;
-		NumericMatrix::Row first  = m.row(0) ;
-		NumericMatrix::Row second = m.row(1) ;
-		first = second ;
-		return R_NilValue ;
-	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
-	x <- matrix( 1:16 + .5, ncol = 4 )
-	funx( x )
-	checkEquals( x[1,], x[2,], msg = "row(0) = row(1)" )
-}
-
-
 test.CharacterMatrix.row <- function(){
 	funx <- cfunction(signature(x = "matrix" ), '
 		CharacterVector m(x) ;

Modified: pkg/Rcpp/src/CharacterVector.cpp
===================================================================
--- pkg/Rcpp/src/CharacterVector.cpp	2010-02-24 18:44:33 UTC (rev 785)
+++ pkg/Rcpp/src/CharacterVector.cpp	2010-02-25 09:26:46 UTC (rev 786)
@@ -49,93 +49,96 @@
 
 /* proxy stuff */
 
-CharacterVector::StringProxy::StringProxy(CharacterVector& v, int i) :
+internal::VectorElement_Proxy<STRSXP>::VectorElement_Proxy(CharacterVector& v, int i) :
 	parent(v), index(i){}
 
-CharacterVector::StringProxy::StringProxy(const StringProxy& other) :
+internal::VectorElement_Proxy<STRSXP>::VectorElement_Proxy(const VectorElement_Proxy& other) :
 	parent(other.parent), index(other.index){}
 
-CharacterVector::StringProxy::operator SEXP() const{
+internal::VectorElement_Proxy<STRSXP>::VectorElement_Proxy::operator SEXP() const{
 	return STRING_ELT( parent, index ) ;
 }
 
-CharacterVector::StringProxy::operator /*const*/ char*() const {
+internal::VectorElement_Proxy<STRSXP>::VectorElement_Proxy::operator /*const*/ char*() const {
 	return const_cast<char*>( CHAR(STRING_ELT( parent, index )) );
 }
 
-// CharacterVector::StringProxy::operator std::string() const {
+// CharacterVector::Proxy::operator std::string() const {
 // 	return std::string( CHAR(STRING_ELT( parent, index )) );
 // }
 
-CharacterVector::StringProxy& CharacterVector::StringProxy::operator=( const StringProxy& rhs){
+internal::VectorElement_Proxy<STRSXP>& internal::VectorElement_Proxy<STRSXP>::VectorElement_Proxy::operator=( const VectorElement_Proxy& rhs){
 	SET_STRING_ELT( parent, index, STRING_ELT( rhs.parent, rhs.index) ) ;
 	return *this ;
 }
 
-CharacterVector::StringProxy& CharacterVector::StringProxy::operator+=( const std::string& rhs){
+internal::VectorElement_Proxy<STRSXP>& internal::VectorElement_Proxy<STRSXP>::VectorElement_Proxy::operator+=( const std::string& rhs){
 	std::string full( CHAR(STRING_ELT(parent,index)) ) ;
 	full += rhs ;
 	SET_STRING_ELT( parent, index, Rf_mkChar( full.c_str() ) ) ;
 	return *this ;
 }
 
-CharacterVector::StringProxy& CharacterVector::StringProxy::operator+=( const StringProxy& rhs){
+internal::VectorElement_Proxy<STRSXP>& internal::VectorElement_Proxy<STRSXP>::VectorElement_Proxy::operator+=( const VectorElement_Proxy& rhs){
 	std::string full( CHAR(STRING_ELT(parent,index)) ) ;
 	full += CHAR(STRING_ELT( rhs.parent, rhs.index)) ;
 	SET_STRING_ELT( parent, index, Rf_mkChar(full.c_str()) ) ;
 	return *this ;
 }
 
-CharacterVector::StringProxy& CharacterVector::StringProxy::operator=( const std::string& rhs){
+internal::VectorElement_Proxy<STRSXP>& internal::VectorElement_Proxy<STRSXP>::VectorElement_Proxy::operator=( const std::string& rhs){
 	SET_STRING_ELT( parent, index, Rf_mkChar( rhs.c_str() ) ) ;
 	return *this ;
 }
 
-std::ostream& operator<<(std::ostream& os, const CharacterVector::StringProxy& proxy) {
-    os << CHAR(STRING_ELT( proxy.parent, proxy.index )) ;
+std::ostream& operator<<(std::ostream& os, const internal::VectorElement_Proxy<STRSXP>& proxy) {
+    os << std::string(proxy) ;
     return os;
 }
 
-const CharacterVector::StringProxy CharacterVector::operator[](int i) const throw(index_out_of_bounds){
-	return StringProxy(const_cast<CharacterVector&>(*this), offset(i) ) ;
+const CharacterVector::Proxy CharacterVector::operator[](int i) const throw(index_out_of_bounds){
+	return Proxy(const_cast<CharacterVector&>(*this), offset(i) ) ;
 }                                          
 
-CharacterVector::StringProxy CharacterVector::operator[](const std::string& name) throw(index_out_of_bounds) {
-	return StringProxy(*this, offset(name) ) ;
+CharacterVector::Proxy CharacterVector::operator[](const std::string& name) throw(index_out_of_bounds) {
+	return Proxy(*this, offset(name) ) ;
 }
 
-const CharacterVector::StringProxy CharacterVector::operator[](const std::string& name) const throw(index_out_of_bounds){
-	return StringProxy(const_cast<CharacterVector&>(*this), offset(name) ) ;
+const CharacterVector::Proxy CharacterVector::operator[](const std::string& name) const throw(index_out_of_bounds){
+	return Proxy(const_cast<CharacterVector&>(*this), offset(name) ) ;
 }                                          
 
-CharacterVector::StringProxy CharacterVector::operator[](int i) throw(index_out_of_bounds) {
-	return StringProxy(*this, offset(i) ) ;
+CharacterVector::Proxy CharacterVector::operator[](int i) throw(index_out_of_bounds) {
+	return Proxy(*this, offset(i) ) ;
 }
 
 
-CharacterVector::StringProxy CharacterVector::operator()( const size_t& i) throw(index_out_of_bounds){
-	return StringProxy(*this, offset(i) ) ;
+CharacterVector::Proxy CharacterVector::operator()( const size_t& i) throw(index_out_of_bounds){
+	return Proxy(*this, offset(i) ) ;
 }
 
-CharacterVector::StringProxy CharacterVector::operator()( const size_t& i, const size_t&j ) throw(index_out_of_bounds,not_a_matrix){
-	return StringProxy(*this, offset(i,j) ) ;
+CharacterVector::Proxy CharacterVector::operator()( const size_t& i, const size_t&j ) throw(index_out_of_bounds,not_a_matrix){
+	return Proxy(*this, offset(i,j) ) ;
 }
 
-std::string operator+( const std::string& x, const CharacterVector::StringProxy& y ){
-	return x + static_cast<const char*>(y) ;
-}
-
-void CharacterVector::StringProxy::swap( StringProxy& other){
+void internal::VectorElement_Proxy<STRSXP>::swap( VectorElement_Proxy& other){
 	SEXP tmp = PROTECT( STRING_ELT(parent, index)) ;
 	SET_STRING_ELT( parent, index, STRING_ELT(other.parent, other.index) ) ;
 	SET_STRING_ELT( other.parent, other.index, tmp ) ;
 	UNPROTECT(1) ;
 }
 
-} // namespace 
+} // namespace Rcpp
 
+std::string operator+( const std::string& x, const Rcpp::internal::VectorElement_Proxy<STRSXP>& y ){
+	return x + static_cast<const char*>(y) ;
+}
+
 namespace std{
-	template<> void swap<Rcpp::CharacterVector::StringProxy>( Rcpp::CharacterVector::StringProxy& a, Rcpp::CharacterVector::StringProxy& b){
+	template<> 
+	void swap< Rcpp::internal::VectorElement_Proxy<STRSXP> >( 
+		Rcpp::internal::VectorElement_Proxy<STRSXP>& a, 
+		Rcpp::internal::VectorElement_Proxy<STRSXP>& b){
 	a.swap(b) ;
 }
 } ;

Modified: pkg/Rcpp/src/Rcpp/CharacterVector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/CharacterVector.h	2010-02-24 18:44:33 UTC (rev 785)
+++ pkg/Rcpp/src/Rcpp/CharacterVector.h	2010-02-25 09:26:46 UTC (rev 786)
@@ -32,20 +32,11 @@
 
 namespace Rcpp{ 
 
-/**
- * Representation of character vectors (STRSXP)
- */
-class CharacterVector : public VectorBase<CharacterVector> {     
-public:
-
-	const static int r_type = STRSXP ;
-	typedef VectorBase<CharacterVector> Base ;
+	class CharacterVector ;
 	
-	/**
-	 * Proxy object that can be used to get or set the value
-	 * a single value of the character vector
-	 */
-	class StringProxy {
+namespace internal{
+	
+	template<> class VectorElement_Proxy<STRSXP>{
 	public:
 		/**
 		 * Creates a proxy
@@ -53,8 +44,8 @@
 		 * @param v reference to the associated character vector
 		 * @param index index 
 		 */
-		StringProxy( CharacterVector& v, int index ) ;
-		StringProxy( const StringProxy& other ) ;
+		VectorElement_Proxy( CharacterVector& v, int index ) ;
+		VectorElement_Proxy( const VectorElement_Proxy& other ) ;
 		
 		/**
 		 * lhs use. Assign the value of the referred element to 
@@ -63,7 +54,7 @@
 		 *
 		 * @param rhs another proxy, possibly from another vector
 		 */
-		StringProxy& operator=(const StringProxy& rhs) ;
+		VectorElement_Proxy& operator=(const VectorElement_Proxy& rhs) ;
 		
 		/**
 		 * lhs use. Assigns the value of the referred element
@@ -71,18 +62,18 @@
 		 *
 		 * @param rhs new content for the element referred by this proxy
 		 */
-		StringProxy& operator=(const std::string& rhs) ;
+		VectorElement_Proxy& operator=(const std::string& rhs) ;
 		
 		/**
 		 * lhs use. Adds the content of the rhs proxy to the 
 		 * element this proxy refers to.
 		 */
-		StringProxy& operator+=(const StringProxy& rhs) ;
+		VectorElement_Proxy& operator+=(const VectorElement_Proxy& rhs) ;
 		
 		/**
 		 * lhs use. Adds the string to the element this proxy refers to
 		 */
-		StringProxy& operator+=(const std::string& rhs) ;
+		VectorElement_Proxy& operator+=(const std::string& rhs) ;
 		
 		/**
 		 * rhs use. Retrieves the current value of the 
@@ -101,23 +92,37 @@
 		 * Prints the element this proxy refers to to an 
 		 * output stream
 		 */
-		friend std::ostream& operator<<(std::ostream& os, const StringProxy& proxy);
+		friend std::ostream& operator<<(std::ostream& os, const VectorElement_Proxy& proxy);
 		
-		void swap( StringProxy& other ) ;
+		void swap( VectorElement_Proxy& other ) ;
+		friend class CharacterVector ;
+		
 	private:
 		CharacterVector& parent; 
 	public:	
 		int index ;
 		inline void move( int n ){ index += n ;}
-		
 	} ;
 	
-	typedef internal::Proxy_Iterator<CharacterVector,StringProxy> iterator ;
+}
 	
-	typedef StringProxy value_type ;
+	
+/**
+ * Representation of character vectors (STRSXP)
+ */
+class CharacterVector : public VectorBase<CharacterVector> {     
+public:
+
+	const static int r_type = STRSXP ;
+	typedef VectorBase<CharacterVector> Base ;
+	
+	typedef internal::VectorElement_Proxy<STRSXP> Proxy ;
+	typedef internal::Proxy_Iterator<CharacterVector,Proxy> iterator ;
+	
+	typedef Proxy value_type ;
 	typedef MatrixRow<CharacterVector> Row ;
 	typedef MatrixColumn<CharacterVector> Column ;
-	typedef StringProxy reference ;
+	typedef Proxy reference ;
 	
 	/**
 	 * Default constructor. Sets the underlying object to NULL
@@ -196,19 +201,17 @@
 	 * Returns a proxy to the given element of the character vector
 	 * The proxy can then be used to get or set the undelting value
 	 */
-	const StringProxy operator[]( int i ) const throw(index_out_of_bounds);
+	const Proxy operator[]( int i ) const throw(index_out_of_bounds);
 	
 	/**
 	 * Returns a proxy to the given element of the character vector
 	 * The proxy can then be used to get or set the undelting value
 	 */
-	StringProxy operator[]( int i ) throw(index_out_of_bounds);
+	Proxy operator[]( int i ) throw(index_out_of_bounds);
 
-	const StringProxy operator[]( const std::string& name) const throw(index_out_of_bounds); 
-	StringProxy operator[]( const std::string& name ) throw(index_out_of_bounds);
+	const Proxy operator[]( const std::string& name) const throw(index_out_of_bounds); 
+	Proxy operator[]( const std::string& name ) throw(index_out_of_bounds);
 
-	friend class StringProxy;
-	
 	/* '(' indexing */
 	/**
 	 * Returns a proxy to the given element of the character vector
@@ -216,7 +219,7 @@
 	 *
 	 * @throw index_out_of_bounds when the given index is invalid
 	 */
-	StringProxy operator()( const size_t& i) throw(index_out_of_bounds) ;
+	Proxy operator()( const size_t& i) throw(index_out_of_bounds) ;
 	
 	/**
 	 * Returns a proxy to the given element of the character vector, 
@@ -227,7 +230,7 @@
 	 * @throw not_a_matrix if the underlying object is not a matrix
 	 * @throw index_out_of_bounds when the given indices do not produce a valid offset
 	 */
-	StringProxy operator()( const size_t& i, const size_t& j) throw(index_out_of_bounds,not_a_matrix) ;
+	Proxy operator()( const size_t& i, const size_t& j) throw(index_out_of_bounds,not_a_matrix) ;
 
 	inline iterator begin() { return iterator(*this, 0 ) ; }
 	
@@ -257,12 +260,14 @@
 
 typedef CharacterVector StringVector ;
 
-std::string operator+( const std::string& x, const CharacterVector::StringProxy& y ) ;
+std::string operator+( const std::string& x, const Rcpp::internal::VectorElement_Proxy<STRSXP>& y ) ;
 
 } // namespace
 
 namespace std{
-	template<> void swap<Rcpp::CharacterVector::StringProxy>(Rcpp::CharacterVector::StringProxy& a, Rcpp::CharacterVector::StringProxy& b) ;
+	template<> void swap< Rcpp::internal::VectorElement_Proxy<STRSXP> >(
+		Rcpp::internal::VectorElement_Proxy<STRSXP>& a, 
+		Rcpp::internal::VectorElement_Proxy<STRSXP>& b) ;
 }
 
 

Modified: pkg/Rcpp/src/Rcpp/MatrixColumn.h
===================================================================
--- pkg/Rcpp/src/Rcpp/MatrixColumn.h	2010-02-24 18:44:33 UTC (rev 785)
+++ pkg/Rcpp/src/Rcpp/MatrixColumn.h	2010-02-25 09:26:46 UTC (rev 786)
@@ -44,9 +44,8 @@
 	MatrixColumn( const MatrixColumn& other ) : parent(other.parent), index(other.index){} ;
 	
 	MatrixColumn& operator=( MatrixColumn& other ){
-		if( other.size() != size() ) throw std::range_error( "not compatible" ) ;
-		std::copy( other.begin(), other.end(), begin() ) ;
-		return *this ;
+		parent = other.parent ;
+		index  = other.index ;
 	}
 	
 	reference operator[]( const int& i ){

Modified: pkg/Rcpp/src/Rcpp/MatrixRow.h
===================================================================
--- pkg/Rcpp/src/Rcpp/MatrixRow.h	2010-02-24 18:44:33 UTC (rev 785)
+++ pkg/Rcpp/src/Rcpp/MatrixRow.h	2010-02-25 09:26:46 UTC (rev 786)
@@ -89,8 +89,8 @@
 	MatrixRow( const MatrixRow& other ) : parent(other.parent), index(other.index){} ;
 	                          
 	MatrixRow& operator=( MatrixRow& other ){
-		if( other.size() != size() ) throw std::range_error( "not compatible" ) ;
-		std::copy( other.begin(), other.end(), begin() ) ;
+		parent = other.parent ;
+		index = other.index ;
 		return *this ;
 	}
 	

Modified: pkg/Rcpp/src/Rcpp/VectorBase.h
===================================================================
--- pkg/Rcpp/src/Rcpp/VectorBase.h	2010-02-24 18:44:33 UTC (rev 785)
+++ pkg/Rcpp/src/Rcpp/VectorBase.h	2010-02-25 09:26:46 UTC (rev 786)
@@ -42,6 +42,15 @@
 	template<> struct r_vector_iterator<STRSXP> ;
 } // traits 
 
+namespace internal{
+	
+	template <int RTYPE> class VectorElement_Proxy {} ;
+	template<> class VectorElement_Proxy<VECSXP> ;
+	template<> class VectorElement_Proxy<EXPRSXP> ;
+	template<> class VectorElement_Proxy<STRSXP> ;
+	
+}
+
 template <typename VECTOR>
 class VectorBase : public RObject {     
 public:



More information about the Rcpp-commits mailing list