[Rcpp-commits] r785 - in pkg/Rcpp/src: . Rcpp Rcpp/internal

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Feb 24 19:44:33 CET 2010


Author: romain
Date: 2010-02-24 19:44:33 +0100 (Wed, 24 Feb 2010)
New Revision: 785

Added:
   pkg/Rcpp/src/Rcpp/internal/Proxy_Iterator.h
Modified:
   pkg/Rcpp/src/CharacterVector.cpp
   pkg/Rcpp/src/Rcpp/CharacterVector.h
   pkg/Rcpp/src/Rcpp/SEXP_Vector.h
   pkg/Rcpp/src/Rcpp/SimpleVector.h
   pkg/Rcpp/src/Rcpp/VectorBase.h
   pkg/Rcpp/src/Rcpp/internal/r_vector.h
   pkg/Rcpp/src/RcppCommon.h
Log:
added internal::Proxy_Iterator template to generate CharacterVector::iterator and SEXP_VEctor::iterator

Modified: pkg/Rcpp/src/CharacterVector.cpp
===================================================================
--- pkg/Rcpp/src/CharacterVector.cpp	2010-02-24 12:58:03 UTC (rev 784)
+++ pkg/Rcpp/src/CharacterVector.cpp	2010-02-24 18:44:33 UTC (rev 785)
@@ -24,38 +24,28 @@
 
 namespace Rcpp{
 
-CharacterVector::CharacterVector() : VectorBase<CharacterVector>(){}
+CharacterVector::CharacterVector() : Base(){}
 
-CharacterVector::CharacterVector(const CharacterVector& other) : VectorBase<CharacterVector>(){
-	setSEXP( other.asSexp() ); 
-}
+CharacterVector::CharacterVector(const CharacterVector& other) : Base(other.asSexp()){}
 
 CharacterVector& CharacterVector::operator=(const CharacterVector& other ){
 	setSEXP( other.asSexp() ) ;
 	return *this ;
 }
 
-CharacterVector::CharacterVector(SEXP x) throw(not_compatible) : VectorBase<CharacterVector>() {
-	SEXP y = r_cast<STRSXP>( x) ;
-	setSEXP( y ) ;
-}
+CharacterVector::CharacterVector(SEXP x) throw(not_compatible) : Base(x) {}
 
-CharacterVector::CharacterVector(const size_t& size) : VectorBase<CharacterVector>(){
-	setSEXP( Rf_allocVector( STRSXP, size ) ) ;
-}
+CharacterVector::CharacterVector(const size_t& size) : Base(size){}
 
-CharacterVector::CharacterVector( const std::string& x) : VectorBase<CharacterVector>() {
-	setSEXP( Rf_mkString(x.c_str()) ) ;
+CharacterVector::CharacterVector( const std::string& x) : Base(1) {
+	SET_STRING_ELT( m_sexp, 0, Rf_mkChar( x.c_str() ) ) ;
 }
 
-CharacterVector::CharacterVector( const std::vector<std::string>& x): VectorBase<CharacterVector>() {
+CharacterVector::CharacterVector( const std::vector<std::string>& x): Base() {
 	assign( x.begin(), x.end() ) ;
 }
 
-CharacterVector::CharacterVector( const Dimension& dims): VectorBase<CharacterVector>(){
-	setSEXP( Rf_allocVector( STRSXP, dims.prod() ) ) ;
-	if( dims.size() > 1 ) attr( "dim" ) = dims ;
-}
+CharacterVector::CharacterVector( const Dimension& dims): Base(dims){}
 
 /* proxy stuff */
 
@@ -131,95 +121,6 @@
 	return StringProxy(*this, offset(i,j) ) ;
 }
 
-/* iterator  */
-
-CharacterVector::iterator::iterator(CharacterVector& object, int index_ ) : 
-	proxy(object,index_) {};
-
-CharacterVector::iterator& CharacterVector::iterator::operator++(){
-	proxy.move(1) ;
-	return *this ;
-}
-
-CharacterVector::iterator& CharacterVector::iterator::operator++(int){
-	proxy.move(1) ;
-	return *this ;
-}
-
-CharacterVector::iterator& CharacterVector::iterator::operator--(){
-	proxy.move(-1) ;
-	return *this ;
-}
-
-CharacterVector::iterator& CharacterVector::iterator::operator--(int){
-	proxy.move(-1) ;
-	return *this ;
-}
-
-CharacterVector::iterator CharacterVector::iterator::operator+( difference_type n) const{
-	return iterator( proxy.parent, proxy.index+n) ;    
-}
-
-CharacterVector::iterator CharacterVector::iterator::operator-( difference_type n) const{
-	return iterator( proxy.parent, proxy.index-n) ;
-}
-
-CharacterVector::iterator& CharacterVector::iterator::operator+=(difference_type n) {
-	proxy.move(n) ;
-	return *this ;
-}
-
-CharacterVector::iterator& CharacterVector::iterator::operator-=(difference_type n) {
-	proxy.move(-n) ;
-	return *this ;
-}
-
-CharacterVector::iterator::reference CharacterVector::iterator::operator*(){
-	return proxy ;
-}
-
-CharacterVector::iterator::pointer CharacterVector::iterator::operator->(){
-	return &proxy ;
-}
-
-bool CharacterVector::iterator::operator==( const CharacterVector::iterator& y){
-	return ( this->proxy.index == y.proxy.index ) && ( this->proxy.parent == y.proxy.parent );
-}
-
-bool CharacterVector::iterator::operator!=( const CharacterVector::iterator& y){
-	return ( this->proxy.index != y.proxy.index ) || ( this->proxy.parent != y.proxy.parent );
-}
-
-bool CharacterVector::iterator::operator<( const iterator& other){
-	/* TODO: deal with the case where this os not iterating over the 
-	         same character vector */
-	return proxy.index < other.proxy.index ;
-}
-
-bool CharacterVector::iterator::operator>( const iterator& other){
-	/* TODO: deal with the case where this os not iterating over the 
-	         same character vector */
-	return proxy.index > other.proxy.index ;
-}
-
-bool CharacterVector::iterator::operator<=( const iterator& other){
-	/* TODO: deal with the case where this os not iterating over the 
-	         same character vector */
-	return proxy.index <= other.proxy.index ;
-}
-
-bool CharacterVector::iterator::operator>=( const iterator& other){
-	/* TODO: deal with the case where this os not iterating over the 
-	         same character vector */
-	return proxy.index >= other.proxy.index ;
-}
-
-
-CharacterVector::iterator::difference_type CharacterVector::iterator::operator-(
-		const CharacterVector::iterator& y ){
-	return y.proxy.index - this->proxy.index ;
-}
-
 std::string operator+( const std::string& x, const CharacterVector::StringProxy& y ){
 	return x + static_cast<const char*>(y) ;
 }

Modified: pkg/Rcpp/src/Rcpp/CharacterVector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/CharacterVector.h	2010-02-24 12:58:03 UTC (rev 784)
+++ pkg/Rcpp/src/Rcpp/CharacterVector.h	2010-02-24 18:44:33 UTC (rev 785)
@@ -38,7 +38,8 @@
 class CharacterVector : public VectorBase<CharacterVector> {     
 public:
 
-	class iterator ;
+	const static int r_type = STRSXP ;
+	typedef VectorBase<CharacterVector> Base ;
 	
 	/**
 	 * Proxy object that can be used to get or set the value
@@ -102,54 +103,17 @@
 		 */
 		friend std::ostream& operator<<(std::ostream& os, const StringProxy& proxy);
 		
-		friend class iterator ;
-		
 		void swap( StringProxy& other ) ;
-		
 	private:
 		CharacterVector& parent; 
+	public:	
 		int index ;
 		inline void move( int n ){ index += n ;}
-	} ;
-
-	class iterator {
-	public:
-		typedef StringProxy& reference ;
-		typedef StringProxy* pointer ;
-		typedef int difference_type ;
-		typedef StringProxy value_type;
-		typedef std::random_access_iterator_tag iterator_category ;
 		
-		iterator( CharacterVector& object, int index );
-		
-		iterator& operator++(); // prefix
-		iterator& operator++(int); // postfix
-		
-		iterator& operator--(); // prefix
-		iterator& operator--(int); // postfix
-		                    
-		iterator operator+(difference_type n) const ;
-		iterator operator-(difference_type n) const ;
-		
-		iterator& operator+=(difference_type n) ;
-		iterator& operator-=(difference_type n) ;
-
-		reference operator*() ;
-		pointer operator->();
-		
-		bool operator==( const iterator& y) ;
-		bool operator!=( const iterator& y) ;
-		bool operator<( const iterator& other ) ;
-		bool operator>( const iterator& other ) ;
-		bool operator<=( const iterator& other ) ;
-		bool operator>=( const iterator& other ) ;
-		
-		difference_type operator-(const iterator& y) ;
-		
-	private:
-		StringProxy proxy ;
-	};
+	} ;
 	
+	typedef internal::Proxy_Iterator<CharacterVector,StringProxy> iterator ;
+	
 	typedef StringProxy value_type ;
 	typedef MatrixRow<CharacterVector> Row ;
 	typedef MatrixColumn<CharacterVector> Column ;
@@ -213,7 +177,7 @@
 	 * over std::string
 	 */
 	template <typename InputIterator>
-	CharacterVector( InputIterator first, InputIterator last): VectorBase<CharacterVector>() {
+	CharacterVector( InputIterator first, InputIterator last): Base() {
 		assign( first, last ) ;
 	}
 	
@@ -223,7 +187,7 @@
 	 *
 	 * Example: CharacterVector x = { "foo", "bar" } ;
 	 */
-	CharacterVector( std::initializer_list<std::string> list ) : VectorBase() {
+	CharacterVector( std::initializer_list<std::string> list ) : Base() {
 		assign( list.begin(), list.end() ) ;
 	}
 #endif

Modified: pkg/Rcpp/src/Rcpp/SEXP_Vector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/SEXP_Vector.h	2010-02-24 12:58:03 UTC (rev 784)
+++ pkg/Rcpp/src/Rcpp/SEXP_Vector.h	2010-02-24 18:44:33 UTC (rev 785)
@@ -35,8 +35,7 @@
 template <int RTYPE>
 class SEXP_Vector : public VectorBase< SEXP_Vector<RTYPE> > {
 public:
-	
-	class iterator ;
+	const static int r_type = RTYPE ;
 	class NameProxy ;
 	class Proxy ;
 	typedef VectorBase< SEXP_Vector<RTYPE> > Base ;
@@ -73,54 +72,17 @@
 			UNPROTECT(1) ;
 		}
 		
-		friend class iterator ;
 	private:
 		SEXP_Vector& parent; 
+	public:
 		size_t index ;
 		inline void move(int n) { index += n ; }
+	private:
 		inline void set(SEXP x) { SET_VECTOR_ELT( parent, index, x ) ;} 
 		inline SEXP get() const { return VECTOR_ELT(parent, index ); } 
 	} ;
 	
-	class iterator {
-	public:
-		typedef Proxy& reference ;
-		typedef Proxy* pointer ;
-		typedef int difference_type ;
-		typedef Proxy value_type;
-		typedef std::random_access_iterator_tag iterator_category ;
-		
-		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; }
-		
-		inline iterator& operator--() { proxy.move(-1) ; return *this; }
-		inline iterator& operator--(int) { proxy.move(-1) ; return *this; }
-		                    
-		inline iterator operator+(difference_type n) const { return iterator( proxy.parent, proxy.index + n ) ; }
-		inline iterator operator-(difference_type n) const { return iterator( proxy.parent, proxy.index - n ) ; }
-		
-		inline iterator& operator+=(difference_type n) { proxy.move(n) ; return *this; }
-		inline iterator& operator-=(difference_type n) { proxy.move(-n) ; return *this; }
-
-		inline reference operator*() { return proxy ; }
-		inline pointer operator->(){ return &proxy ; }
-		
-		inline bool operator==( const iterator& y) { return this->proxy.index == y.proxy.index && this->proxy.parent == y.proxy.parent; }
-		inline bool operator!=( const iterator& y) { return this->proxy.index != y.proxy.index || this->proxy.parent != y.proxy.parent; }
-		inline bool operator< ( const iterator& y) { return this->proxy.index <  y.proxy.index ; }
-		inline bool operator> ( const iterator& y) { return this->proxy.index >  y.proxy.index ; }
-		inline bool operator<=( const iterator& y) { return this->proxy.index <= y.proxy.index ; }
-		inline bool operator>=( const iterator& y) { return this->proxy.index >= y.proxy.index ; }
-		
-		inline difference_type operator-(const iterator& y) { return this->proxy.index - y.proxy.index ; }
-		
-		inline int index(){ return proxy.index ; }
-		
-	private:
-		Proxy proxy ;
-	};
+	typedef internal::Proxy_Iterator<SEXP_Vector<RTYPE>,Proxy> iterator ;
 	
 	class NameProxy {
 	public:
@@ -168,7 +130,6 @@
 	} ;
 	
 	friend class Proxy;
-	friend class iterator ;
 	friend class NameProxy ;
 	
 	typedef MatrixRow<SEXP_Vector> Row ;
@@ -192,14 +153,9 @@
 		Base::setSEXP( y );
 	}
 	
-	SEXP_Vector(const size_t& size) : Base(){
-		Base::setSEXP( ::Rf_allocVector( RTYPE, size ) ) ;
-	}
+	SEXP_Vector(const size_t& size) : Base(size){}
 	
-	SEXP_Vector(const Dimension& dims) : Base(){
-		Base::setSEXP( ::Rf_allocVector( RTYPE, dims.prod() ) ) ;
-		if( dims.size() > 1) Base::attr( "dim" ) = dims ;
-	}
+	SEXP_Vector(const Dimension& dims) : Base(dims){}
 
 	template <typename InputIterator>
 	SEXP_Vector(InputIterator first, InputIterator last) : Base() {

Modified: pkg/Rcpp/src/Rcpp/SimpleVector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/SimpleVector.h	2010-02-24 12:58:03 UTC (rev 784)
+++ pkg/Rcpp/src/Rcpp/SimpleVector.h	2010-02-24 18:44:33 UTC (rev 785)
@@ -36,35 +36,20 @@
 public:
 	
 	typedef VectorBase< SimpleVector<RTYPE> > Base ;
+	const static int r_type = RTYPE ;
 	typedef typename traits::storage_type<RTYPE>::type value_type ;
 	typedef value_type* iterator ;
 	typedef value_type& reference ;
-	typedef MatrixRow<SimpleVector> Row ;
-	typedef MatrixColumn<SimpleVector> Column ;
 	
 	SimpleVector() : Base(), start(0){}
 	
-	SimpleVector(SEXP x) throw(RObject::not_compatible) : Base(), start(0){
-		SEXP y = r_cast<RTYPE>( x ) ;
-		Base::setSEXP( y );
-	}
+	SimpleVector(SEXP x) throw(RObject::not_compatible) : Base(x){}
 	
-	SimpleVector( const size_t& size) :Base() {
-		Base::setSEXP( Rf_allocVector( RTYPE, size) ) ;
-		init() ;
-	}
+	SimpleVector( const size_t& size) : Base(size) {}
 	
-	SimpleVector( const Dimension& dims) : Base() {
-		Base::setSEXP( Rf_allocVector( RTYPE, dims.prod() ) ) ;
-		init() ;
-		if( dims.size() > 1 ){
-			Base::attr( "dim" ) = dims ;
-		}
-	}
+	SimpleVector( const Dimension& dims) : Base(dims) {}
 	
-	SimpleVector( const SimpleVector& other) : Base() {
-		Base::setSEXP( other.asSexp() ) ;
-	}
+	SimpleVector( const SimpleVector& other) : Base( other.asSexp() ) {}
 	
 	SimpleVector& operator=(const SimpleVector& other){
 		Base::setSEXP( other.asSexp() ) ;
@@ -114,9 +99,7 @@
 		UNPROTECT(1) ;
 	}
 	
-	inline Row row( int i ){ return Row( *this, i ) ; }
-	inline Column column( int i ){ return Column( *this, i ) ; }
-
+	
 protected:
 	void init(){
 		internal::r_init_vector<RTYPE>(Base::m_sexp) ;

Modified: pkg/Rcpp/src/Rcpp/VectorBase.h
===================================================================
--- pkg/Rcpp/src/Rcpp/VectorBase.h	2010-02-24 12:58:03 UTC (rev 784)
+++ pkg/Rcpp/src/Rcpp/VectorBase.h	2010-02-24 18:44:33 UTC (rev 785)
@@ -26,16 +26,50 @@
 #include <Rcpp/exceptions.h>
 #include <Rcpp/RObject.h>
 #include <Rcpp/r_cast.h>
+#include <Rcpp/Dimension.h>
+#include <Rcpp/MatrixRow.h>
+#include <Rcpp/MatrixColumn.h>
 
 namespace Rcpp{ 
 
+namespace traits{
+	template <int RTYPE>
+	struct r_vector_iterator {
+		typedef typename storage_type<RTYPE>::type* iterator ;
+	} ;
+	template<> struct r_vector_iterator<VECSXP> ;
+	template<> struct r_vector_iterator<EXPRSXP> ;
+	template<> struct r_vector_iterator<STRSXP> ;
+} // traits 
+
 template <typename VECTOR>
 class VectorBase : public RObject {     
 public:
+
+	const static int r_type = VECTOR::r_type ;
+	typedef MatrixRow<VECTOR> Row ;
+	typedef MatrixColumn<VECTOR> Column ;
 	
     VectorBase() : RObject(){} ;
     virtual ~VectorBase(){};
     
+    VectorBase( SEXP x ) : RObject(){
+    	setSEXP( r_cast<r_type>( x ) ) ;
+    }
+    
+    VectorBase( const size_t& size ) : RObject(){
+    	setSEXP( Rf_allocVector( r_type, size) ) ;
+		init() ;
+    }
+    
+    VectorBase( const Dimension& dims) : RObject(){
+    	setSEXP( Rf_allocVector( r_type, dims.prod() ) ) ;
+		init() ;
+		if( dims.size() > 1 ){
+			attr( "dim" ) = dims;
+		}
+    }
+    
     /**
      * the length of the vector, uses Rf_length
      */
@@ -138,6 +172,9 @@
 		return NamesProxy(*this) ;
 	}
     
+	inline Row row( int i ){ return Row( static_cast<VECTOR&>(*this), i ) ; }
+	inline Column column( int i ){ return Column( static_cast<VECTOR&>(*this), i ) ; }
+	
 private:
 		
 	inline int* dims(){
@@ -145,6 +182,10 @@
 		return INTEGER( ::Rf_getAttrib( m_sexp, ::Rf_install( "dim") ) ) ;
 	}
 	
+	void init(){
+		internal::r_init_vector<r_type>(m_sexp) ;
+	}
+	
 } ;
 
 } // namespace

Added: pkg/Rcpp/src/Rcpp/internal/Proxy_Iterator.h
===================================================================
--- pkg/Rcpp/src/Rcpp/internal/Proxy_Iterator.h	                        (rev 0)
+++ pkg/Rcpp/src/Rcpp/internal/Proxy_Iterator.h	2010-02-24 18:44:33 UTC (rev 785)
@@ -0,0 +1,112 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */
+//
+// Proxy_Iterator.h: Rcpp R/C++ interface class library -- 
+//
+// 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/>.
+
+#ifndef Rcpp__internal__Proxy_Iterator__h
+#define Rcpp__internal__Proxy_Iterator__h
+
+namespace Rcpp{
+namespace internal{
+
+template <typename VECTOR, typename PROXY>
+class Proxy_Iterator {
+public:
+		typedef PROXY& reference ;
+		typedef PROXY* pointer ;
+		typedef int difference_type ;
+		typedef PROXY value_type;
+		typedef std::random_access_iterator_tag iterator_category ;
+		
+		Proxy_Iterator( VECTOR& object, int index ) : proxy( object, index){} ;
+		
+		inline Proxy_Iterator& operator++(){
+			proxy.move(1) ;
+			return *this ;
+		}
+		inline Proxy_Iterator& operator++(int){
+			proxy.move(1) ;
+			return *this ;
+		}
+		
+		inline Proxy_Iterator& operator--(){
+			proxy.move(-1) ;
+			return *this ;
+		}
+		inline Proxy_Iterator& operator--(int){
+			proxy.move(-1) ;
+			return *this ;
+		}
+		                    
+		inline Proxy_Iterator operator+(difference_type n) const {
+			return iterator( proxy.parent, proxy.index + n) ;
+		}
+		inline Proxy_Iterator operator-(difference_type n) const {
+			return iterator( proxy.parent, proxy.index - n) ;
+		}
+		
+		inline Proxy_Iterator& operator+=(difference_type n) {
+			proxy.move( n ) ;
+			return *this ;
+		}
+		inline Proxy_Iterator& operator-=(difference_type n) {
+			proxy.move( -n ) ;
+			return *this ;
+		}
+
+		inline reference operator*() {
+			return proxy ;
+		}
+		inline pointer operator->(){
+			return &proxy ;
+		}
+		
+		inline bool operator==( const Proxy_Iterator& y) {
+			return ( this->proxy.index == y.proxy.index ) && ( this->proxy.parent == y.proxy.parent );
+		}
+		inline bool operator!=( const Proxy_Iterator& y) {
+			return ( this->proxy.index != y.proxy.index ) || ( this->proxy.parent != y.proxy.parent );
+		}
+		inline bool operator<( const Proxy_Iterator& other ) {
+			return proxy.index < other.proxy.index ;
+		}
+		inline bool operator>( const Proxy_Iterator& other ) {
+			return proxy.index > other.proxy.index ;
+		}
+		inline bool operator<=( const Proxy_Iterator& other ) {
+			return proxy.index <= other.proxy.index ;
+		}
+		inline bool operator>=( const Proxy_Iterator& other ) {
+			return proxy.index >= other.proxy.index ;
+		}
+		
+		inline difference_type operator-(const Proxy_Iterator& other) {
+			return other.proxy.index - proxy.index ;
+		}
+
+private:
+	PROXY proxy ;
+} ;
+
+}
+}
+
+#endif
+

Modified: pkg/Rcpp/src/Rcpp/internal/r_vector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/internal/r_vector.h	2010-02-24 12:58:03 UTC (rev 784)
+++ pkg/Rcpp/src/Rcpp/internal/r_vector.h	2010-02-24 18:44:33 UTC (rev 785)
@@ -72,6 +72,6 @@
 template<> void r_init_vector<STRSXP>(SEXP x) ;
 
 } // internal
-} // internal
+} // Rcpp
 
 #endif

Modified: pkg/Rcpp/src/RcppCommon.h
===================================================================
--- pkg/Rcpp/src/RcppCommon.h	2010-02-24 12:58:03 UTC (rev 784)
+++ pkg/Rcpp/src/RcppCommon.h	2010-02-24 18:44:33 UTC (rev 785)
@@ -180,6 +180,7 @@
 #include <Rcpp/internal/wrap.h>
 
 #include <Rcpp/internal/ListInitialization.h>
+#include <Rcpp/internal/Proxy_Iterator.h>
 
 RcppExport SEXP RcppXPtrExample_create_external_pointer() ;
 RcppExport SEXP RcppXPtrExample_get_external_pointer(SEXP ); 



More information about the Rcpp-commits mailing list