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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Feb 24 13:15:16 CET 2010


Author: romain
Date: 2010-02-24 13:15:16 +0100 (Wed, 24 Feb 2010)
New Revision: 782

Removed:
   pkg/Rcpp/src/SEXP_Vector.cpp
Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/unitTests/runit.Column.R
   pkg/Rcpp/src/Rcpp/MatrixColumn.h
   pkg/Rcpp/src/Rcpp/SEXP_Vector.h
   pkg/Rcpp/src/Rcpp/traits/Exporter.h
Log:
remove the intermediate SEXP_Vector_Base class

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-02-24 11:27:03 UTC (rev 781)
+++ pkg/Rcpp/inst/ChangeLog	2010-02-24 12:15:16 UTC (rev 782)
@@ -1,5 +1,10 @@
 2010-02-24  Romain Francois <romain at r-enthusiasts.com>
 
+	* src/Rcpp/SEXP_Vector.h: the intermediate class 
+	SEXP_Vector_Base is removed, reverting the change on 
+	2010-02-09. This will facilitate the introduction of the 
+	Curiously recurring template pattern on vectors
+
 	* src/Rcpp/MatrixRow.h : Row has been factored out of SimpleVector
 	into a separate template class Rcpp::MatrixRow
 	

Modified: pkg/Rcpp/inst/unitTests/runit.Column.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Column.R	2010-02-24 11:27:03 UTC (rev 781)
+++ pkg/Rcpp/inst/unitTests/runit.Column.R	2010-02-24 12:15:16 UTC (rev 782)
@@ -24,7 +24,7 @@
 		return wrap( std::accumulate( col.begin(), col.end(), 0.0 ) ) ;
 	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
 	x <- matrix( 1:16 + .5, ncol = 4 )
-	checkEquals( funx( x ), sum( x[,1], msg = "iterating over a column" )
+	checkEquals( funx( x ), sum( x[,1] ) , msg = "iterating over a column" )
 }
 
 test.NumericMatrix.column.operator.equal <- function(){
@@ -51,7 +51,7 @@
 	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
 	
 	m <- matrix( letters, ncol = 2 )
-	checkEquals( funx(m), paste( m[1,], collapse = "" ), msg = "CharacterVector::Column" )
+	checkEquals( funx(m), paste( m[,1], collapse = "" ), msg = "CharacterVector::Column" )
 }
 
 test.List.column <- function(){

Modified: pkg/Rcpp/src/Rcpp/MatrixColumn.h
===================================================================
--- pkg/Rcpp/src/Rcpp/MatrixColumn.h	2010-02-24 11:27:03 UTC (rev 781)
+++ pkg/Rcpp/src/Rcpp/MatrixColumn.h	2010-02-24 12:15:16 UTC (rev 782)
@@ -53,12 +53,12 @@
 		return parent[ index * parent.ncol() + i ] ;
 	}
 	
-	inline iterator begin(){
-		return iterator( parent, index * parent.ncol() ) ;
+	iterator begin(){
+		return parent.begin() + index * parent.ncol() ;
 	}
 	
-	inline iterator end(){
-		return iterator( parent, index * parent.ncol() + parent.nrow() ) ;
+	iterator end(){
+		return parent.begin() + index * parent.ncol() + parent.nrow() ;
 	}
 	
 	inline int size() const {

Modified: pkg/Rcpp/src/Rcpp/SEXP_Vector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/SEXP_Vector.h	2010-02-24 11:27:03 UTC (rev 781)
+++ pkg/Rcpp/src/Rcpp/SEXP_Vector.h	2010-02-24 12:15:16 UTC (rev 782)
@@ -31,19 +31,28 @@
 
 namespace Rcpp{
 
-class SEXP_Vector_Base : public VectorBase {
+template <int RTYPE>
+class SEXP_Vector : public VectorBase {
 public:
+	
 	class iterator ;
 	class NameProxy ;
+	class Proxy ;
 	
 	class Proxy {
 	public:
-		Proxy( SEXP_Vector_Base& v, size_t i ) ;
+		Proxy( SEXP_Vector& v, size_t i ) : parent(v), index(i){} ;
 		
-		Proxy& operator=(SEXP rhs) ; 
+		Proxy& operator=(SEXP rhs) { 
+			set(rhs) ;
+			return *this ;
+		}
 		
-		Proxy& operator=(const Proxy& rhs) ;
-		
+		Proxy& operator=(const Proxy& rhs) {
+			set(rhs.get());
+			return *this ;	
+		}
+               
 		template <typename T>
 		Proxy& operator=( const T& rhs){
 			set( wrap(rhs) ) ;
@@ -55,15 +64,20 @@
 			return as<U>( get() ) ;
 		}
 		
-		void swap(Proxy& other) ;
+		void swap(Proxy& other){
+			SEXP tmp = PROTECT( get() ) ;
+			set( other.get() ) ;
+			other.set( tmp ) ;
+			UNPROTECT(1) ;
+		}
 		
 		friend class iterator ;
 	private:
-		SEXP_Vector_Base& parent; 
+		SEXP_Vector& parent; 
 		size_t index ;
-		void move(int n) ;
-		void set(SEXP x) ;
-		SEXP get() const ;
+		inline void move(int n) { index += n ; }
+		inline void set(SEXP x) { SET_VECTOR_ELT( parent, index, x ) ;} 
+		inline SEXP get() const { return VECTOR_ELT(parent, index ); } 
 	} ;
 	
 	class iterator {
@@ -74,7 +88,7 @@
 		typedef Proxy value_type;
 		typedef std::random_access_iterator_tag iterator_category ;
 		
-		iterator( SEXP_Vector_Base& object, int index );
+		iterator( SEXP_Vector& object, int index ) : proxy(object,index){}
 		
 		inline iterator& operator++(){ proxy.move(1) ; return *this; }
 		inline iterator& operator++(int){ proxy.move(1) ; return *this; }
@@ -106,21 +120,6 @@
 		Proxy proxy ;
 	};
 	
-	SEXP_Vector_Base() ; 
-	
-	friend class Proxy;
-	friend class iterator ;
-	
-} ;
-
-template <int RTYPE>
-class SEXP_Vector : public SEXP_Vector_Base{
-public:
-	typedef MatrixRow<SEXP_Vector> Row ;
-	typedef MatrixColumn<SEXP_Vector> Column ;
-	typedef Proxy reference ;
-	typedef Proxy value_type ;
-	
 	class NameProxy {
 	public:
 		NameProxy( SEXP_Vector& v, const std::string& name_ ) : parent(v), name(name_){} ;
@@ -166,9 +165,18 @@
 		}
 	} ;
 	
-	SEXP_Vector() : SEXP_Vector_Base(){} ; 
+	friend class Proxy;
+	friend class iterator ;
+	friend class NameProxy ;
 	
-	SEXP_Vector(const SEXP_Vector& other) : SEXP_Vector_Base(){
+	typedef MatrixRow<SEXP_Vector> Row ;
+	typedef MatrixColumn<SEXP_Vector> Column ;
+	typedef Proxy reference ;
+	typedef Proxy value_type ;
+	
+	SEXP_Vector() : VectorBase(){} ; 
+	
+	SEXP_Vector(const SEXP_Vector& other) : VectorBase(){
 		setSEXP( other.asSexp() ) ;
 	} ;
 	
@@ -177,27 +185,27 @@
 		return *this ;
 	}
 	
-	SEXP_Vector(SEXP x) : SEXP_Vector_Base() {
+	SEXP_Vector(SEXP x) : VectorBase() {
 		SEXP y = r_cast<RTYPE>(x) ;
 		setSEXP( y );
 	}
 	
-	SEXP_Vector(const size_t& size) : SEXP_Vector_Base(){
+	SEXP_Vector(const size_t& size) : VectorBase(){
 		setSEXP( Rf_allocVector( RTYPE, size ) ) ;
 	}
 	
-	SEXP_Vector(const Dimension& dims) : SEXP_Vector_Base(){
+	SEXP_Vector(const Dimension& dims) : VectorBase(){
 		setSEXP( Rf_allocVector( RTYPE, dims.prod() ) ) ;
 		if( dims.size() > 1) attr( "dim" ) = dims ;
 	}
 
 	template <typename InputIterator>
-	SEXP_Vector(InputIterator first, InputIterator last) : SEXP_Vector_Base() {
+	SEXP_Vector(InputIterator first, InputIterator last) : VectorBase() {
 		assign( first, last ) ;
 	}
 	
 #ifdef HAS_INIT_LISTS
-	SEXP_Vector( std::initializer_list<SEXP> list) : SEXP_Vector_Base(){
+	SEXP_Vector( std::initializer_list<SEXP> list) : VectorBase(){
 		assign( list.begin(), list.end() ) ;
 	} ;
 #endif
@@ -210,7 +218,7 @@
 	}
     	
 	inline const Proxy operator[]( int i ) const throw(index_out_of_bounds){
-		return Proxy(const_cast<SEXP_Vector_Base&>(*this), offset(i)) ;
+		return Proxy(const_cast<SEXP_Vector&>(*this), offset(i)) ;
 	}
 	inline Proxy operator[]( int i ) throw(index_out_of_bounds){
 		return Proxy(*this, offset(i) ) ; 
@@ -440,7 +448,8 @@
 } //namespace Rcpp
 
 namespace std {
-	template<> void swap( Rcpp::SEXP_Vector_Base::Proxy& a, Rcpp::SEXP_Vector_Base::Proxy& b) ;
+	template<> inline void swap(Rcpp::SEXP_Vector<VECSXP>::Proxy& a, Rcpp::SEXP_Vector<VECSXP>::Proxy& b) { a.swap(b) ; }
+	template<> inline void swap(Rcpp::SEXP_Vector<EXPRSXP>::Proxy& a, Rcpp::SEXP_Vector<EXPRSXP>::Proxy& b) { a.swap(b) ; }
 }
 
 #endif

Modified: pkg/Rcpp/src/Rcpp/traits/Exporter.h
===================================================================
--- pkg/Rcpp/src/Rcpp/traits/Exporter.h	2010-02-24 11:27:03 UTC (rev 781)
+++ pkg/Rcpp/src/Rcpp/traits/Exporter.h	2010-02-24 12:15:16 UTC (rev 782)
@@ -28,7 +28,7 @@
 
 template <typename T> class Exporter{
 public:
-	Exporter( SEXP x ) : T(x){}
+	Exporter( SEXP x ) : t(x){}
 	inline T get(){ return t ; }
 
 private:

Deleted: pkg/Rcpp/src/SEXP_Vector.cpp
===================================================================
--- pkg/Rcpp/src/SEXP_Vector.cpp	2010-02-24 11:27:03 UTC (rev 781)
+++ pkg/Rcpp/src/SEXP_Vector.cpp	2010-02-24 12:15:16 UTC (rev 782)
@@ -1,75 +0,0 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
-//
-// Symbol.cpp: Rcpp R/C++ interface class library -- Symbols
-//
-// 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/>.
-
-#include <Rcpp/SEXP_Vector.h>
-
-namespace Rcpp{
-
-/* proxy */
-	
-SEXP_Vector_Base::Proxy::Proxy(SEXP_Vector_Base& v, size_t i):
-	parent(v), index(i) {};
-
-void SEXP_Vector_Base::Proxy::set(SEXP x){
-	SET_VECTOR_ELT( parent, index, x ) ;
-}
-
-SEXP SEXP_Vector_Base::Proxy::get() const {
-	return VECTOR_ELT(parent, index );
-}
-
-SEXP_Vector_Base::Proxy& SEXP_Vector_Base::Proxy::operator=(SEXP rhs){
-	set(rhs); 
-	return *this ;
-}
-
-SEXP_Vector_Base::Proxy& SEXP_Vector_Base::Proxy::operator=(const Proxy& rhs){
-	set(rhs.get());
-	return *this ;
-}
-
-void SEXP_Vector_Base::Proxy::swap(Proxy& other){
-	SEXP tmp = PROTECT( get() ) ;
-	set( other.get() ) ;
-	other.set( tmp ) ;
-	UNPROTECT(1) ;
-}
-
-void SEXP_Vector_Base::Proxy::move(int n){
-	index += n ;
-}
-
-/* iterator */
-
-SEXP_Vector_Base::iterator::iterator( SEXP_Vector_Base& object, int index_):
-	proxy(object,index_){}
-
-/* SEXP_Vector_Base */
-SEXP_Vector_Base::SEXP_Vector_Base() : VectorBase(){}
-
-} // namespace Rcpp
-
-namespace std{
-template<> void swap( Rcpp::SEXP_Vector_Base::Proxy& a, Rcpp::SEXP_Vector_Base::Proxy& b){
-	a.swap(b) ;
-}
-}
-



More information about the Rcpp-commits mailing list