[Rcpp-commits] r1557 - in pkg/Rcpp/inst: . include/Rcpp include/Rcpp/vector

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 17 10:09:58 CEST 2010


Author: romain
Date: 2010-06-17 10:09:58 +0200 (Thu, 17 Jun 2010)
New Revision: 1557

Added:
   pkg/Rcpp/inst/include/Rcpp/vector/
   pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
   pkg/Rcpp/inst/include/Rcpp/vector/MatrixColumn.h
   pkg/Rcpp/inst/include/Rcpp/vector/MatrixRow.h
   pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
   pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h
   pkg/Rcpp/inst/include/Rcpp/vector/converter.h
   pkg/Rcpp/inst/include/Rcpp/vector/eval_methods.h
   pkg/Rcpp/inst/include/Rcpp/vector/instantiation.h
   pkg/Rcpp/inst/include/Rcpp/vector/proxy.h
   pkg/Rcpp/inst/include/Rcpp/vector/string_proxy.h
   pkg/Rcpp/inst/include/Rcpp/vector/traits.h
Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/Vector.h
Log:
split Vector.h into more manageable files

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-06-17 07:09:23 UTC (rev 1556)
+++ pkg/Rcpp/inst/ChangeLog	2010-06-17 08:09:58 UTC (rev 1557)
@@ -1,3 +1,7 @@
+2010-06-17  Romain Francois <romain at r-enthusiasts.com>
+
+	* inst/include/Rcpp/Vector.h: split into more manageable files
+
 2010-06-16  Dirk Eddelbuettel  <edd at debian.org>
 
 	* src/RcppDateVector.cpp: Also provide non-const operator()(int i)

Modified: pkg/Rcpp/inst/include/Rcpp/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Vector.h	2010-06-17 07:09:23 UTC (rev 1556)
+++ pkg/Rcpp/inst/include/Rcpp/Vector.h	2010-06-17 08:09:58 UTC (rev 1557)
@@ -35,1570 +35,21 @@
 template <int RTYPE> class MatrixRow ;
 template <int RTYPE> class MatrixColumn ;
 
-namespace internal{
-	template <int RTYPE> class string_proxy ;
-	template <int RTYPE> class generic_proxy ;
-	
-	template <int RTYPE> class simple_name_proxy {
-	public:
-		typedef ::Rcpp::Vector<RTYPE> VECTOR ;
-		typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE ;
-		simple_name_proxy( VECTOR& v, const std::string& name_) :
-			parent(v), name(name_){} ;
-		simple_name_proxy( const simple_name_proxy& other ) : 
-			parent(other.parent), name(other.name){} ;
-		~simple_name_proxy() {} ;
-		
-		simple_name_proxy& operator=( CTYPE rhs ){
-			set( rhs ) ;
-			return *this ;
-		}
-		simple_name_proxy& operator=( const simple_name_proxy& other){
-			set( other.get() ) ;
-			return *this ;
-		}
-		
-		template <typename T>
-		simple_name_proxy& operator=( const T& rhs ){
-			set( caster<T,CTYPE>(rhs) ) ;
-			return *this ;
-		}
-		
-		// TODO: other operators +=, -=, ...
-		
-		operator CTYPE() const {
-			 return get() ;
-		}
-		
-		// this helps wrap, for example : wrap( x["foo"] )
-		operator SEXP() const {
-			return ::Rcpp::wrap(get()) ;
-		}
-		
-	private:
-		VECTOR& parent ;
-		std::string name;
-		void set( CTYPE rhs ){
-			int index = 0 ;
-			try{
-				index = parent.offset(name) ;
-				parent[ index ] = rhs ;
-			} catch( const index_out_of_bounds& ex ){
-				parent.push_back( rhs, name ); 
-			}
-		}
-		CTYPE get() const {
-			return parent[ parent.offset(name) ];
-		}
-	} ;
-	
-	template <int RTYPE>
-	class string_name_proxy{
-	public:
-		typedef typename ::Rcpp::Vector<RTYPE> VECTOR ;
-		typedef const char* iterator ;
-		typedef const char& reference ;
-		
-		string_name_proxy( VECTOR& v, const std::string& name_) :
-			parent(v), name(name_){} ;
-		string_name_proxy( const string_name_proxy& other ) : 
-			parent(other.parent), name(other.name){} ;
-		~string_name_proxy(){} ;
-		
-		string_name_proxy& operator=( const std::string& rhs ){
-			set( rhs ) ;
-			return *this ;
-		}
-		string_name_proxy& operator=( const string_name_proxy& other){
-			set( other.get() ) ;
-			return *this ;
-		}
-		
-		operator char* (){
-			 return get() ;
-		}
-		
-		operator SEXP(){
-			return ::Rf_mkString(get()) ;
-		}
-		
-		inline iterator begin() { return get() ; }
-		inline iterator end(){ return begin() + size() ; }
-		inline reference operator[]( int i ){ return *( get() + i ) ; }
-		inline int size(){ return strlen( get() ) ; }
-		
-	private:
-		VECTOR& parent ;
-		std::string name;
-		void set( const std::string& rhs ){
-			int index = 0 ;
-			try{
-				index = parent.offset(name) ;
-				parent[ index ] = rhs ;
-			} catch( const index_out_of_bounds& ex ){
-				parent.push_back( rhs, name ); 
-			}
-		}
-		char* get(){
-			return parent[ parent.offset(name) ];
-		}
-		
-	} ;
-	
-	template <int RTYPE> class generic_name_proxy {
-	public:
-		typedef ::Rcpp::Vector<RTYPE> VECTOR ;
-		generic_name_proxy( VECTOR& v, const std::string& name_) :
-			parent(v), name(name_){
-				RCPP_DEBUG( "generic_name_proxy( VECTOR& = %p, const string& = %s)", v.asSexp(), name_.c_str() );
-		} ;
-		generic_name_proxy( const generic_name_proxy& other ) : 
-			parent(other.parent), name(other.name){} ;
-		~generic_name_proxy(){} ;
-		
-		generic_name_proxy& operator=( SEXP rhs ){
-			set( rhs ) ;
-			return *this ;
-		}
-		generic_name_proxy& operator=( const generic_name_proxy& other){
-			set( other.get() ) ;
-			return *this ;
-		}
-		
-		template <typename T>
-		generic_name_proxy& operator=( const T& rhs ){
-			set( ::Rcpp::wrap(rhs) ) ;
-			return *this ;
-		}
-		
-		// TODO: other operators +=, -=, ...
-		
-		operator SEXP(){
-			 return get() ;
-		}
-		
-		template <typename T>
-		operator T(){
-			#if RCPP_DEBUG_LEVEL > 0
-			SEXP res = get() ;
-			RCPP_DEBUG( "generic_name_proxy::get() = <%p> ", res ) ;
-			return ::Rcpp::as<T>( res ) ;
-			#else
-			return ::Rcpp::as<T>( get() ) ;
-			#endif
-		}
-		
-	private:
-		VECTOR& parent ;
-		std::string name;
-		void set( SEXP rhs ){
-			int index = 0 ;
-			try{
-				index = parent.offset(name) ;
-				parent[ index ] = rhs ;
-			} catch( const index_out_of_bounds& ex ){
-				parent.push_back( rhs, name ); 
-			}
-		}
-		SEXP get(){
-			return parent[ parent.offset(name) ];
-		}
-	} ;
-	
-	template <int RTYPE>
-	class element_converter{
-	public:
-		typedef typename ::Rcpp::traits::storage_type<RTYPE>::type target ;
-		
-		template <typename T>
-		static target get( const T& input ){
-			return caster<T,target>(input) ;
-		}
-		
-		static target get( const target& input ){
-			return input ;
-		}
-	} ;
-	
-	template <int RTYPE>
-	class string_element_converter {
-	public:
-		typedef SEXP target ;
-		
-		template <typename T>
-		static SEXP get( const T& input){
-			std::string out(input) ;
-			return Rf_mkChar( out.c_str() ) ;
-		}
-		
-		static SEXP get(const std::string& input){
-			return Rf_mkChar( input.c_str() ) ;
-		}
-		
-		static SEXP get(const char& input){
-			return Rf_mkChar( &input ) ;
-		}
-	} ;
-	
-	template <int RTYPE>
-	class generic_element_converter {
-	public:
-		typedef SEXP target ;
-		
-		template <typename T>
-		static SEXP get( const T& input){
-			return ::Rcpp::wrap( input ) ;
-		}
-		
-		static SEXP get( const char* input){
-			return ::Rcpp::wrap( input );
-		}
-		
-		static SEXP get(SEXP input){
-			return input ;
-		}
-	} ;
-	
-	template <int RTYPE>
-	SEXP vector_from_string( const std::string& st ) throw(parse_error,not_compatible) {
-		return r_cast<RTYPE>( Rf_mkString( st.c_str() ) ) ;
-	}
-	
-	template <int RTYPE>
-	SEXP vector_from_string_expr( const std::string& code) throw(parse_error,not_compatible) {
-		ParseStatus status;
-		SEXP expr = PROTECT( ::Rf_mkString( code.c_str() ) );
-		SEXP res  = PROTECT( ::R_ParseVector(expr, -1, &status, R_NilValue));
-		switch( status ){
-		case PARSE_OK:
-			UNPROTECT( 2) ;
-			return(res) ;
-			break;
-		default:
-			UNPROTECT(2) ;
-			throw parse_error() ;
-		}
-		return R_NilValue ; /* -Wall */
-	}
-	
-	template <>
-	inline SEXP vector_from_string<EXPRSXP>( const std::string& st ) throw(parse_error,not_compatible) {
-		return vector_from_string_expr<EXPRSXP>( st ) ;
-	}
-	
-	template <typename VECTOR> class eval_methods {} ;
-	
-	template <typename VECTOR> class expr_eval_methods {
-	public:
-		SEXP eval(){
-			SEXP xp = ( static_cast<VECTOR&>(*this) ).asSexp() ;
-			return try_catch( ::Rf_lcons( ::Rf_install( "eval" ) , ::Rf_cons( xp, R_NilValue) ) ) ;
-		} ;
-		SEXP eval( const ::Rcpp::Environment& env ){
-			SEXP xp = ( static_cast<VECTOR&>(*this) ).asSexp() ;
-			return try_catch( ::Rf_lcons( ::Rf_install( "eval" ) , ::Rf_cons( xp, ::Rf_cons(env.asSexp(), R_NilValue)) ) ) ;
-		} ;
-	} ;
-	
-	template<> class eval_methods< ::Rcpp::Vector<EXPRSXP> > : 
-		public expr_eval_methods< ::Rcpp::Vector<EXPRSXP> > {} ;
-	
-}
+#include <Rcpp/vector/proxy.h>
+#include <Rcpp/vector/converter.h>
+#include <Rcpp/vector/eval_methods.h>
+#include <Rcpp/vector/traits.h>
 
-namespace traits{
+#include <Rcpp/vector/VectorBase.h>
+#include <Rcpp/vector/Vector.h>
+#include <Rcpp/vector/Matrix.h>
+#include <Rcpp/vector/MatrixRow.h>
+#include <Rcpp/vector/MatrixColumn.h>
 
-	template <int RTYPE> struct r_vector_element_converter{
-		typedef typename ::Rcpp::internal::element_converter<RTYPE> type ;
-	} ;
-	template<> struct r_vector_element_converter<STRSXP>{
-		typedef ::Rcpp::internal::string_element_converter<STRSXP> type ;
-	} ;
-	template<> struct r_vector_element_converter<VECSXP>{
-		typedef ::Rcpp::internal::generic_element_converter<VECSXP> type ;
-	} ;
-	template<> struct r_vector_element_converter<EXPRSXP>{
-		typedef ::Rcpp::internal::generic_element_converter<EXPRSXP> type ;
-	} ;
-	
-	template <int RTYPE> 
-	struct r_vector_name_proxy{
-		typedef typename ::Rcpp::internal::simple_name_proxy<RTYPE> type ;
-	} ;
-	template<> struct r_vector_name_proxy<STRSXP>{
-		typedef ::Rcpp::internal::string_name_proxy<STRSXP> type ;
-	} ;
-	template<> struct r_vector_name_proxy<VECSXP>{
-		typedef ::Rcpp::internal::generic_name_proxy<VECSXP> type ;
-	} ;
-	template<> struct r_vector_name_proxy<EXPRSXP>{
-		typedef ::Rcpp::internal::generic_name_proxy<EXPRSXP> type ;
-	} ;
-	
-	template <int RTYPE>
-	struct r_vector_proxy{
-		typedef typename storage_type<RTYPE>::type& type ;
-	} ;
-	template<> struct r_vector_proxy<STRSXP> {
-		typedef ::Rcpp::internal::string_proxy<STRSXP> type ;
-	} ;
-	template<> struct r_vector_proxy<EXPRSXP> {
-		typedef ::Rcpp::internal::generic_proxy<EXPRSXP> type ;
-	} ;
-	template<> struct r_vector_proxy<VECSXP> {
-		typedef ::Rcpp::internal::generic_proxy<VECSXP> type ;
-	} ;
-	
-	template <int RTYPE>
-	struct r_vector_iterator {
-		typedef typename storage_type<RTYPE>::type* type ;
-	};
-	template <int RTYPE> struct proxy_based_iterator{
-		typedef ::Rcpp::internal::Proxy_Iterator< typename r_vector_proxy<RTYPE>::type > type ;
-	} ;
-	template<> struct r_vector_iterator<VECSXP> : proxy_based_iterator<VECSXP>{} ;
-	template<> struct r_vector_iterator<EXPRSXP> : proxy_based_iterator<EXPRSXP>{} ;
-	template<> struct r_vector_iterator<STRSXP> : proxy_based_iterator<STRSXP>{} ;
-	
-	template <int RTYPE>
-	class r_vector_cache{
-	public:
-		typedef typename ::Rcpp::Vector<RTYPE> VECTOR ;
-		typedef typename r_vector_iterator<RTYPE>::type iterator ;
-		typedef typename r_vector_proxy<RTYPE>::type proxy ;
-		typedef typename storage_type<RTYPE>::type storage_type ;
-		
-		r_vector_cache() : start(0){} ;
-		void update( const VECTOR& v ) {
-			start = ::Rcpp::internal::r_vector_start<RTYPE,storage_type>(v.asSexp()) ;
-		}
-		inline iterator get() const { return start; }
-		inline iterator get(int i) const { return start + i ; }
-		inline proxy ref() const { return *start ;}
-		inline proxy ref(int i) const { return *(start+i) ; }
-		
-		private:
-			iterator start ;
-	} ;
-	template <int RTYPE> class proxy_cache{
-	public:
-		typedef typename ::Rcpp::Vector<RTYPE> VECTOR ;
-		typedef typename r_vector_iterator<RTYPE>::type iterator ;
-		typedef typename r_vector_proxy<RTYPE>::type proxy ;
-		
-		proxy_cache(): p(0){}
-		~proxy_cache(){}
-		void update( const VECTOR& v ){
-			p = const_cast<VECTOR*>(&v) ;
-		}
-		inline iterator get() const { return iterator( proxy(*p, 0 ) ) ;}
-		inline iterator get(int i) const { return iterator( proxy(*p, i ) ) ;}
-		inline proxy ref() const { return proxy(*p,0) ; }
-		inline proxy ref(int i) const { return proxy(*p,i);}
-		
-		private:
-			VECTOR* p ;
-	} ;
-	template<> class r_vector_cache<VECSXP> : public proxy_cache<VECSXP>{
-	public:
-		r_vector_cache() : proxy_cache<VECSXP>(){} ;
-	};
-	template<> class r_vector_cache<EXPRSXP> : public proxy_cache<EXPRSXP>{
-	public:
-		r_vector_cache() : proxy_cache<EXPRSXP>(){} ;
-	};
-	template<> class r_vector_cache<STRSXP> : public proxy_cache<STRSXP>{
-	public:
-		r_vector_cache() : proxy_cache<STRSXP>(){} ;
-	} ;
-	
-	template<int RTYPE> struct init_type {
-		typedef typename storage_type<RTYPE>::type type ;
-	} ;
-	template<> struct init_type<STRSXP>{
-		typedef const char* type ;
-	} ;
-	template<> struct init_type<LGLSXP>{
-		typedef bool type ;
-	} ;
-	
-	template <int RTYPE> struct is_trivial  : public true_type{} ;
-	template <> struct is_trivial<VECSXP>   : public false_type{} ;
-	template <> struct is_trivial<EXPRSXP>  : public false_type{} ;
+#include <Rcpp/vector/instantiation.h>
 
-} // traits 
+#include <Rcpp/vector/string_proxy.h>
 
-template <typename VECTOR>
-class VectorBase : public RObject{
-public:
-	VectorBase() : RObject(){
-		RCPP_DEBUG( "VectorBase()", 0 ) ;
-	}
-	VectorBase(SEXP x) : RObject(x){
-		update() ;
-		RCPP_DEBUG( "VectorBase( SEXP = <%p> ) = <%p>", x, asSexp() ) ;
-	}
-	~VectorBase(){
-		RCPP_DEBUG( "~VectorBase", 0 ) ;
-	}
-	VectorBase(const VectorBase& v) : RObject( v.asSexp() ){
-		update() ;
-		RCPP_DEBUG( "VectorBase( const VectorBase& = <%p> ) ) = <%p>", v.asSexp(), asSexp() ) ;
-	}
-	VectorBase& operator=(const VectorBase& v) {
-		setSEXP( v.asSexp() ) ;
-		return *this ;
-	}
-	VECTOR& get_ref(){
-		return static_cast<VECTOR&>(*this) ;
-	}
-	virtual void update(){
-		RCPP_DEBUG( "%s::update", DEMANGLE(VectorBase) ) ;
-		(static_cast<VECTOR&>(*this)).update_vector() ;
-	}
-} ;
-
-template <int RTYPE>
-class Vector : public VectorBase< Vector<RTYPE> >, public internal::eval_methods< Vector<RTYPE> >{
-public:
-	typedef VectorBase<Vector> Base ;
-	typedef typename traits::r_vector_proxy<RTYPE>::type Proxy ;
-	typedef typename traits::r_vector_name_proxy<RTYPE>::type NameProxy ;
-	typedef typename traits::r_vector_proxy<RTYPE>::type value_type ;
-	typedef typename traits::r_vector_iterator<RTYPE>::type iterator ;
-	typedef typename traits::init_type<RTYPE>::type init_type ;
-	typedef typename traits::r_vector_element_converter<RTYPE>::type converter_type ;
-	typedef typename traits::storage_type<RTYPE>::type stored_type ;
-	typedef MatrixRow<RTYPE> Row ;
-	typedef MatrixColumn<RTYPE> Column ;
-	
-	struct r_type : traits::integral_constant<int,RTYPE>{} ;
-	
-	Vector() : Base() {
-		RCPP_DEBUG( "Vector()", 0 ) ;
-		Base::setSEXP( Rf_allocVector( RTYPE, 0 ) ) ;
-		init() ;
-	} ;
-    ~Vector(){
-    	RCPP_DEBUG( "~Vector()", 0 ) ;
-	};
-    
-	Vector( const Vector& other) : Base() {
-		Base::setSEXP(other.asSexp()) ;
-	}
-	
-	Vector& operator=( const Vector& other ){
-		Base::setSEXP( other.asSexp() ) ;
-		return *this ;
-	}
-	
-	Vector( const RObject::SlotProxy& proxy ) throw(not_compatible) {
-		Base::setSEXP( r_cast<RTYPE>( proxy ) ) ;
-	}
-	
-	Vector( const RObject::AttributeProxy& proxy ) throw(not_compatible) {
-		Base::setSEXP( r_cast<RTYPE>( proxy ) ) ;
-	}
-	
-	template <typename T>
-	Vector& operator=( const T& x){
-		Base::setSEXP( r_cast<RTYPE>( wrap(x) ) ) ;
-		return *this ;
-	}
-
-	internal::ListInitialization<iterator,init_type> operator=( init_type x){
-		iterator start = begin() ; *start = x; 
-		return internal::ListInitialization<iterator,init_type>( start + 1 ) ; ;
-	}
-	
-    Vector( SEXP x ) throw(not_compatible) : Base() {
-    	RCPP_DEBUG( "Vector<%d>( SEXP = <%p> )", RTYPE, x) ;
-    	Base::setSEXP( r_cast<RTYPE>( x ) ) ;
-    	RCPP_DEBUG( "===========", 0) ;
-    }
-    
-    Vector( const int& size ) : Base()  {
-    	Base::setSEXP( Rf_allocVector( RTYPE, size) ) ;
-		init() ;
-    }
-    
-    template <typename U>
-    Vector( const int& size, const U& u){
-    	Base::setSEXP( Rf_allocVector( RTYPE, size) ) ;
-		fill( u ) ;	
-    }
-    
-    Vector( const Dimension& dims) : Base() {
-    	Base::setSEXP( Rf_allocVector( RTYPE, dims.prod() ) ) ;
-		init() ;
-		if( dims.size() > 1 ){
-			Base::attr( "dim" ) = dims;
-		}
-    }
-    
-    template <typename U>
-    Vector( const Dimension& dims, const U& u) : Base() {
-    	Base::setSEXP( Rf_allocVector( RTYPE, dims.prod() ) ) ;
-		fill(u) ;
-		if( dims.size() > 1 ){
-			Base::attr( "dim" ) = dims;
-		}
-    }
-    
-   
-	template <typename InputIterator>
-	Vector( InputIterator first, InputIterator last) : Base(){
-		assign( first, last ) ;
-	}
-
-	Vector( const std::string& st ) throw(parse_error,not_compatible) : Base(){
-		Base::setSEXP( internal::vector_from_string<RTYPE>(st) );
-	}
-	
-#ifdef HAS_INIT_LISTS
-	Vector( std::initializer_list<init_type> list ) : Base(){
-		assign( list.begin() , list.end() ) ;
-	}
-#endif
-	
-    /**
-     * the length of the vector, uses Rf_length
-     */
-    inline R_len_t length() const { return ::Rf_length( Base::m_sexp ) ; }
-    
-    /**
-     * alias of length
-     */
-    inline R_len_t size() const { return ::Rf_length( Base::m_sexp ) ; }
-    
-    inline int ncol() const throw(not_a_matrix) {
-    	return dims()[1]; 
-    }
-    
-    inline int nrow() const throw(not_a_matrix){
-    	return dims()[0]; 
-    }
-
-    inline int cols() const throw(not_a_matrix){ 
-	return dims()[1]; 
-    }
-    inline int rows() const throw(not_a_matrix){ 
-	return dims()[0]; 
-    }
-	
-    /**
-     * offset based on the dimensions of this vector
-     */
-    size_t offset(const size_t& i, const size_t& j) const throw(not_a_matrix,index_out_of_bounds){
-    	if( !::Rf_isMatrix(Base::m_sexp) ) throw not_a_matrix() ;
-		/* we need to extract the dimensions */
-		int *dim = dims() ;
-		size_t nrow = static_cast<size_t>(dim[0]) ;
-		size_t ncol = static_cast<size_t>(dim[1]) ;
-		if( i >= nrow || j >= ncol ) throw index_out_of_bounds() ;
-		return i + nrow*j ;
-    }
-    
-    /**
-     * one dimensional offset doing bounds checking to ensure
-     * it is valid
-     */
-    size_t offset(const size_t& i) const throw(index_out_of_bounds){
-		if( static_cast<R_len_t>(i) >= ::Rf_length(Base::m_sexp) ) throw index_out_of_bounds() ;
-		return i ;
-    }
-    
-    R_len_t offset(const std::string& name) const throw(index_out_of_bounds){
-    	SEXP names = RCPP_GET_NAMES( Base::m_sexp ) ;
-    	if( names == R_NilValue ) throw index_out_of_bounds(); 
-    	R_len_t n=size() ;
-    	for( R_len_t i=0; i<n; ++i){
-    		if( ! name.compare( CHAR(STRING_ELT(names, i)) ) ){
-    			return i ;
-    		}
-    	}
-    	throw index_out_of_bounds() ;
-    	return -1 ; /* -Wall */
-    }
-    
-    private:
-	
-	template <typename U>
-	void fill_dispatch( traits::false_type, const U& u){
-		// when this is not trivial, this is SEXP
-		SEXP elem = PROTECT( converter_type::get( u ) ); 
-		iterator it(begin());
-		for( int i=0; i<size ; i++, ++it){
-			*it = ::Rf_duplicate( elem ) ;
-		}
-		UNPROTECT(1) ; /* elem */
-	}
-	
-	template <typename U>
-	void fill__dispatch( traits::true_type, const U& u){
-		std::fill( begin(), end(), converter_type::get( u ) ) ;
-	}
-	
-	public:	
-	
-	template <typename U>
-	void fill( const U& u){
-		fill__dispatch( typename traits::is_trivial<RTYPE>::type(), u ) ;
-	}
-
-    
-    /* TODO: 3 dimensions, ... n dimensions through variadic templates */
-    
-    class NamesProxy {
-	public:
-		NamesProxy( const Vector& v) : parent(v){} ;
-	
-		/* lvalue uses */              
-		NamesProxy& operator=(const NamesProxy& rhs) {
-			set( rhs.get() ) ;
-			return *this ;
-    		}
-	
-		template <typename T>
-		NamesProxy& operator=(const T& rhs){
-			set( wrap(rhs) ) ;
-			return *this ;
-		}
-	
-		template <typename T> operator T() const {
-			return Rcpp::as<T>(get()) ;
-		}
-		
-	private:
-		const Vector& parent; 
-		
-		SEXP get() const {
-			return RCPP_GET_NAMES(parent) ;
-		}
-		
-		void set(SEXP x) const {
-			SEXP new_vec = PROTECT( internal::try_catch( 
-			Rf_lcons( Rf_install("names<-"), 
-					Rf_cons( parent, Rf_cons( x , R_NilValue) )))) ;
-			/* names<- makes a new vector, so we have to change 
-			   the SEXP of the parent of this proxy, it might be 
-			   worth to work directly with the names attribute instead
-			   of using the names<- R function, but then we need to 
-			   take care of coercion, recycling, etc ... we cannot just 
-			   brutally assign the names attribute */
-			const_cast<Vector&>(parent).setSEXP( new_vec ) ;
-			UNPROTECT(1) ; /* new_vec */
-    		}
-    		
-	} ;
-    	
-	NamesProxy names() const {
-		return NamesProxy(*this) ;
-	}
-    
-	inline Row row( int i ){ return Row( *this, i ) ; }
-	inline Column column( int i ){ return Column(*this, i ) ; }
-	
-	inline iterator begin() const{ return cache.get() ; }
-	inline iterator end() const{ return cache.get(size()) ; }
-	
-	inline Proxy operator[]( const int& i ){ return cache.ref(i) ; }
-	inline Proxy operator[]( const int& i ) const { return cache.ref(i) ; }
-	inline Proxy operator()( const size_t& i) throw(index_out_of_bounds){
-		return cache.ref( offset(i) ) ;
-	}
-	inline Proxy operator()( const size_t& i, const size_t& j) throw(not_a_matrix,index_out_of_bounds){
-		return cache.ref( offset(i,j) ) ;
-	}
-	inline NameProxy operator[]( const std::string& name ){
-		return NameProxy( *this, name ) ;
-	}
-	inline NameProxy operator()( const std::string& name ){
-		return NameProxy( *this, name ) ;
-	}
-	
-	template <typename InputIterator>
-	void assign( InputIterator first, InputIterator last){
-		/* FIXME: we can do better than this r_cast to avoid 
-		          allocating an unnecessary temporary object
-		 */
-		SEXP x = PROTECT( r_cast<RTYPE>( wrap( first, last ) ) );
-		Base::setSEXP( x) ;
-		UNPROTECT(1) ;
-	}
-
-	template <typename InputIterator>
-	static Vector import( InputIterator first, InputIterator last){
-		Vector v ;
-		v.assign( first , last ) ;
-		return v ;
-	}
-
-	template <typename InputIterator, typename F>
-	static Vector import_transform( InputIterator first, InputIterator last, F f){
-		int n = std::distance( first, last ) ;
-		Vector v( n ) ;
-		std::transform( first, last, v.begin(), f) ;
-		return v ;
-	}
-	
-	template <typename T>
-	void push_back( const T& object){
-		push_back__impl( converter_type::get(object) ) ;
-	}
-	
-	template <typename T>
-	void push_back( const T& object, const std::string& name ){
-		push_back_name__impl( converter_type::get(object), name ) ;
-	}
-	
-	template <typename T>
-	void push_front( const T& object){
-		push_front__impl( converter_type::get(object) ) ;
-	}
-	
-	template <typename T>
-	void push_front( const T& object, const std::string& name){
-		push_front_name__impl( converter_type::get(object), name ) ;
-	}
-	
-	
-	template <typename T>
-	iterator insert( iterator position, const T& object){
-		return insert__impl( position, converter_type::get(object) ) ;
-	}
-	
-	template <typename T>
-	iterator insert( int position, const T& object){
-		return insert__impl( cache.get(position), converter_type::get(object) ); 
-	}
-	
-	iterator erase( int position){
-		return erase_single__impl( cache.get(position) ) ;
-	}
-	
-	iterator erase( iterator position){
-		return erase_single__impl( position ) ;
-	}
-	
-	iterator erase( int first, int last){
-		return erase_range__impl( cache.get(first), cache.get(last) ) ;
-	}
-	
-	iterator erase( iterator first, iterator last){
-		return erase_range__impl( first, last ) ;
-	}
-	
-	void update_vector(){
-		RCPP_DEBUG(  "update_vector, VECTOR = %s", DEMANGLE(Vector) ) ;
-		cache.update(*this) ;
-	}
-		
-	static Vector create(){
-		return Vector( 0 ) ;
-	}
-
-#include <Rcpp/generated/Vector__create.h>
-
-	template <typename U>
-	static void replace_element( iterator it, SEXP names, int index, const U& u){
-		replace_element__dispatch( typename traits::is_named<U>::type(), 
-			it, names, index, u ) ;
-	}
-	
-	template <typename U>
-	static void replace_element__dispatch( traits::false_type, iterator it, SEXP names, int index, const U& u){
-		*it = converter_type::get(u);
-	}
-	
-	template <typename U>
-	static void replace_element__dispatch( traits::true_type, iterator it, SEXP names, int index, const U& u){
-		*it = converter_type::get(u.object ) ;
-		SET_STRING_ELT( names, index, ::Rf_mkChar( u.name.c_str() ) ) ;
-	}
-	
-	
-	void set_sexp(SEXP x){
-		Base::setSEXP( x) ;
-		update_vector() ;
-	}
-	
-	void push_back__impl(const stored_type& object){
-		int n = size() ;
-		Vector target( n + 1 ) ;
-		SEXP names = RCPP_GET_NAMES(Base::m_sexp) ;
-		iterator target_it( target.begin() ) ;
-		iterator it(begin()) ;
-		iterator this_end(end());
-		if( names == R_NilValue ){
-			for( ; it < this_end; ++it, ++target_it ){
-				*target_it = *it ;
-			}
-		} else {
-			SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1) ) ;
-			int i = 0 ;
-			for( ; it < this_end; ++it, ++target_it, i++ ){
-				*target_it = *it ;
-				SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
-			}
-			SET_STRING_ELT( newnames, i, Rf_mkChar("") ) ;
-			target.attr("names") = newnames ;
-			UNPROTECT(1) ; /* newnames */
-		}
-		*target_it = object;
-		set_sexp( target.asSexp() ) ;
-	}
-	
-	void push_back_name__impl(const stored_type& object, const std::string& name ){
-		int n = size() ;
-		Vector target( n + 1 ) ;
-		iterator target_it( target.begin() ) ;
-		iterator it(begin()) ;
-		iterator this_end(end());
-		SEXP names = RCPP_GET_NAMES(Base::m_sexp) ;
-		SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n+1 ) ) ;
-		int i=0;
-		if( names == R_NilValue ){
-			SEXP dummy = PROTECT( Rf_mkChar("") );
-			for( ; it < this_end; ++it, ++target_it,i++ ){
-				*target_it = *it ;
-				SET_STRING_ELT( newnames, i , dummy );
-			}
-			UNPROTECT(1) ; /* dummy */
-		} else {
-			for( ; it < this_end; ++it, ++target_it, i++ ){
-				*target_it = *it ;
-				SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
-			}
-		}
-		SET_STRING_ELT( newnames, i, Rf_mkChar( name.c_str() ) );
-		target.attr("names") = newnames ;
-		
-		*target_it = object;
-		UNPROTECT(1) ; /* newnames, */
-		set_sexp( target.asSexp() ) ;
-	}
-	
-	
-	void push_front__impl(const stored_type& object){
-		int n = size() ;
-		Vector target( n+1);
-		iterator target_it(target.begin());
-		iterator it(begin());
-		iterator this_end(end());
-		*target_it = object ;
-		++target_it ;
-		SEXP names = RCPP_GET_NAMES(Base::m_sexp) ;
-		if( names == R_NilValue ){
-			for( ; it<this_end; ++it, ++target_it){
-				*target_it = *it ;
-			}
-		} else{
-			SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1) );
-			int i=1 ;
-			SET_STRING_ELT( newnames, 0, Rf_mkChar("") ) ;
-			for( ; it<this_end; ++it, ++target_it, i++){
-				*target_it = *it ;
-				SET_STRING_ELT( newnames, i, STRING_ELT(names, i-1 ) ) ;
-			}
-			target.attr("names") = newnames ;
-			UNPROTECT(1) ; /* newnames */
-		}
-		set_sexp( target.asSexp() ) ;
-	}
-	
-	void push_front_name__impl(const stored_type& object, const std::string& name ){
-		int n = size() ;
-		Vector target( n + 1 ) ;
-		iterator target_it( target.begin() ) ;
-		iterator it(begin()) ;
-		iterator this_end(end());
-		SEXP names = RCPP_GET_NAMES(Base::m_sexp) ;
-		SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n+1 ) ) ;
-		int i=1;
-		SET_STRING_ELT( newnames, 0, Rf_mkChar( name.c_str() ) );
-		*target_it = object;
-		++target_it ;
-		
-		if( names == R_NilValue ){
-			SEXP dummy = PROTECT( Rf_mkChar("") );
-			for( ; it < this_end; ++it, ++target_it,i++ ){
-				*target_it = *it ;
-				SET_STRING_ELT( newnames, i , dummy );
-			}
-			UNPROTECT(1) ; /* dummy */
-		} else {
-			for( ; it < this_end; ++it, ++target_it, i++ ){
-				*target_it = *it ;
-				SET_STRING_ELT( newnames, i, STRING_ELT(names, i-1 ) ) ;
-			}
-		}
-		target.attr("names") = newnames ;
-		
-		UNPROTECT(1) ; /* newnames, */
-		set_sexp( target.asSexp() ) ;
-	}
-	
-	
-	
-	iterator insert__impl( iterator position, const stored_type& object){
-		int n = size() ;
-		// iterator start = begin() ;
-		// int pos = position - start ;
-		Vector target( n+1 ) ;
-		iterator target_it = target.begin();
-		iterator it = begin() ;
-		iterator this_end = end() ;
-		SEXP names = RCPP_GET_NAMES(Base::m_sexp) ;
-		iterator result ;
-		if( names == R_NilValue ){
-			for( ; it < position; ++it, ++target_it){
-				*target_it = *it ;
-			}
-			result = target_it;
-			*target_it = object ; 
-			++target_it ;
-			for( ; it < this_end; ++it, ++target_it ){
-				*target_it = *it ;
-			}
-		} else{
-			SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1 ) ) ;
-			int i=0;
-			for( ; it < position; ++it, ++target_it, i++){
-				*target_it = *it ;
-				SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
-			}
-			result = target_it;
-			*target_it = object ;
-			SET_STRING_ELT( newnames, i, ::Rf_mkChar("") ) ;
-			i++ ;
-			++target_it ;
-			for( ; it < this_end; ++it, ++target_it, i++ ){
-				*target_it = *it ;
-				SET_STRING_ELT( newnames, i, STRING_ELT(names, i - 1) ) ;
-			}
-			target.attr( "names" ) = newnames ;
-			UNPROTECT(1) ; /* newmanes */
-		}
-		set_sexp( target.asSexp() );
-		return result ;
-	}
-	
-	iterator erase_single__impl( iterator position ){
-		if( position < begin() || position >= end() ) throw index_out_of_bounds( ) ;
-		int n = size() ;
-		Vector target( n - 1 ) ;
-		iterator target_it(target.begin()) ;
-		iterator it(begin()) ;
-		iterator this_end(end()) ;
-		SEXP names = RCPP_GET_NAMES(Base::m_sexp) ;
-		if( names == R_NilValue ){
-			for( ; it < position; ++it, ++target_it){
-				*target_it = *it;
-			}
-			iterator result(target_it) ;
-			++it ;
-			for( ; it < this_end ; ++it, ++target_it){
-				*target_it = *it;
-			}
-			set_sexp( target.asSexp() ) ;
-			return result ;
-		} else {
-			SEXP newnames = PROTECT(::Rf_allocVector( STRSXP, n-1 ));
-			int i= 0 ;
-			for( ; it < position; ++it, ++target_it,i++){
-				*target_it = *it;
-				SET_STRING_ELT( newnames, i , STRING_ELT(names,i) ) ;
-			}
-			iterator result(target_it) ;
-			++it ;
-			i++ ;
-			for( ; it < this_end ; ++it, ++target_it, i++){
-				*target_it = *it;
-				SET_STRING_ELT( newnames, i-1, STRING_ELT(names,i) ) ;
-			}
-			target.attr( "names" ) = newnames ;
-			UNPROTECT(1) ; /* newnames */
-			set_sexp( target.asSexp() ) ;
-			return result ;
-		}
-	}
-	
-	iterator erase_range__impl( iterator first, iterator last ){
-		if( first > last ) throw std::range_error("invalid range") ;
-		if( last >= end() || first < begin() ) throw index_out_of_bounds() ;
-		
-		iterator it = begin() ;
-		iterator this_end = end() ;
-		int nremoved = std::distance(first,last)+1 ;
-		int target_size = size() - nremoved  ;
-		Vector target( target_size ) ;
-		iterator target_it = target.begin() ;
-		
-		SEXP names = RCPP_GET_NAMES(Base::m_sexp) ;
-		iterator result ;
-		if( names == R_NilValue ){
-			for( ; it < first; ++it, ++target_it ){
-				*target_it = *it ;
-			}
-			result = it ;
-			for( it = last +1 ; it < this_end; ++it, ++target_it ){
-				*target_it = *it ;
-			}
-		} else{
-			SEXP newnames = PROTECT( ::Rf_allocVector(STRSXP, target_size) ) ;
-			int i= 0 ;
-			for( ; it < first; ++it, ++target_it, i++ ){
-				*target_it = *it ;
-				SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) );
-			}
-			result = it ;
-			for( it = last +1 ; it < this_end; ++it, ++target_it, i++ ){
-				*target_it = *it ;
-				SET_STRING_ELT( newnames, i, STRING_ELT(names, i + nremoved ) );
-			}
-			target.attr("names" ) = newnames ;
-			UNPROTECT(1) ; /* newnames */
-		}
-		set_sexp(target.asSexp() );
-		return result ;
-	}
-	
-	inline int* dims() const throw(not_a_matrix) {
-		if( !::Rf_isMatrix(Base::m_sexp) ) throw not_a_matrix() ;
-		return INTEGER( ::Rf_getAttrib( Base::m_sexp, ::Rf_install( "dim") ) ) ;
-	}
-	
-	void init(){
-		internal::r_init_vector<RTYPE>(Base::m_sexp) ;
-	}
-
-	virtual void update(){
-		RCPP_DEBUG( "%s::update", DEMANGLE(Vector) ) ;
-		update_vector() ;
-	}
-	
-	traits::r_vector_cache<RTYPE> cache ;
-
-} ; /* Vector */
-
-template <int RTYPE> 
-class Matrix : public Vector<RTYPE> {
-public:
-	typedef Vector<RTYPE> VECTOR ;
-	typedef typename VECTOR::iterator iterator ;
-    typedef typename VECTOR::converter_type converter_type ;
-    typedef typename VECTOR::stored_type stored_type ;
-    	
-	Matrix() : VECTOR() {}
-	
-	Matrix(SEXP x) throw(not_compatible) : VECTOR(){
-		if( ! ::Rf_isMatrix(x) ) throw not_compatible("not a matrix") ;
-		SEXP y = r_cast<RTYPE>( x ) ;
-		VECTOR::setSEXP( y );
-	}
-	
-	Matrix( const Dimension& dims) throw(not_compatible) : VECTOR() {
-		if( dims.size() != 2 ) throw not_compatible("not a matrix") ;
-		VECTOR::setSEXP( Rf_allocVector( RTYPE, dims.prod() ) ) ;
-		VECTOR::init() ;
-		VECTOR::attr( "dim" ) = dims ;
-	}
-	
-	Matrix( const int& nrows, const int& ncols) : VECTOR( Dimension( nrows, ncols ) ) {}
-	Matrix( const int& n) : VECTOR( Dimension( n, n ) ) {}
-	
-	Matrix( const Matrix& other) throw(not_compatible) : VECTOR() {
-		SEXP x = other.asSexp() ;
-		if( ! ::Rf_isMatrix(x) ) throw not_compatible("not a matrix") ;
-		VECTOR::setSEXP( x ) ;
-	}
-	
-	Matrix& operator=(const Matrix& other) throw(not_compatible) {                                                         
-		SEXP x = other.asSexp() ;
-		if( ! ::Rf_isMatrix(x) ) not_compatible("not a matrix") ;
-		VECTOR::setSEXP( x ) ;
-		return *this ;
-	}
-	
-	template <typename U>
-    void fill_diag( const U& u){
-    	fill_diag__dispatch( typename traits::is_trivial<RTYPE>::type(), u ) ;	
-    }
-	
-	template <typename U>
-    static Matrix diag( int size, const U& diag_value ){
-    	Matrix res(size,size) ;
-    	res.fill_diag( diag_value ) ;
-    	return res ;
-    }
-    
-private:
-	virtual void update(){
-		RCPP_DEBUG( "%s::update", DEMANGLE(Matrix) ) ;
-		VECTOR::update_vector() ;
-	}
-	
-	template <typename U>
-	void fill_diag__dispatch( traits::false_type, const U& u){
-		SEXP elem = PROTECT( converter_type::get( u ) ) ;
-		int n = Matrix::ncol() ;
-		int offset = n +1 ;
-		iterator it( Matrix::begin()) ;
-		for( int i=0; i<n; i++){
-    		*it = ::Rf_duplicate( elem );
-    		it += offset; 
-    	}
-    	UNPROTECT(1); // elem
-	}
-	
-	template <typename U>
-	void fill_diag__dispatch( traits::true_type, const U& u){
-		stored_type elem = converter_type::get( u ) ;
-		int n = Matrix::ncol() ;
-		int offset = n + 1 ;
-		iterator it( Matrix::begin()) ;
-		for( int i=0; i<n; i++){
-    		*it = elem ;
-    		it += offset; 
-    	}
-    }
-	    
-} ;
-
-template <int RTYPE>
-class MatrixRow {
-public:
-	typedef Vector<RTYPE> VECTOR ;
-	typedef typename VECTOR::Proxy Proxy ;
-	typedef typename VECTOR::Proxy reference ;
-	// typedef typename VECTOR::value_type value_type ;
-	
-	class iterator {
-	public:
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/rcpp -r 1557


More information about the Rcpp-commits mailing list