[Rcpp-commits] r779 - 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 24 11:36:46 CET 2010


Author: romain
Date: 2010-02-24 11:36:46 +0100 (Wed, 24 Feb 2010)
New Revision: 779

Added:
   pkg/Rcpp/src/Rcpp/MatrixRow.h
Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/unitTests/runit.Row.R
   pkg/Rcpp/src/Rcpp.h
   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
Log:
+ Rcpp::MatrixRow (factored out of SimpleVector so that it handles character vectors and generic vectors)

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-02-23 12:51:06 UTC (rev 778)
+++ pkg/Rcpp/inst/ChangeLog	2010-02-24 10:36:46 UTC (rev 779)
@@ -1,3 +1,17 @@
+2010-02-24  Romain Francois <romain at r-enthusiasts.com>
+
+	* src/Rcpp/MatrixRow.h : Row has been factored out of SimpleVector
+	into a separate template class Rcpp::MatrixRow
+	
+	* src/Rcpp/SimpleVector.h: gains a row method that returns 
+	a SimpleVector::Row, aka MatrixRow<SimpleVector>
+	
+	* src/Rcpp/CharacterVector.h : gains a row method that returns
+	a CharacterVector::Row, aka MatrixRow<CharacterVector>
+	
+	* src/Rcpp/SEXP_Vector.h : gains a row method that returns 
+	a SEXP_Vector::Row aka MatrixRow<SEXP_Vector>
+
 2010-02-23  Romain Francois <romain at r-enthusiasts.com>
 
 	* src/Rcpp/SimpleVector.h: added the class SimpleVector::Row

Modified: pkg/Rcpp/inst/unitTests/runit.Row.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Row.R	2010-02-23 12:51:06 UTC (rev 778)
+++ pkg/Rcpp/inst/unitTests/runit.Row.R	2010-02-24 10:36:46 UTC (rev 779)
@@ -17,13 +17,46 @@
 # You should have received a copy of the GNU General Public License
 # along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
 
-test.NumericMatrix <- function(){
+test.NumericMatrix.row <- function(){
 	funx <- cfunction(signature(x = "matrix" ), '
 		NumericMatrix m(x) ;
-		NumericMatrix::Row first_row( m, 0 );
+		NumericMatrix::Row first_row = m.row(0) ;
 		return wrap( std::accumulate( first_row.begin(), first_row.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 row" )
 }
 
+test.CharacterMatrix.row <- function(){
+	funx <- cfunction(signature(x = "matrix" ), '
+		CharacterVector m(x) ;
+		CharacterVector::Row first_row = m.row(0) ;
+		std::string res( 
+			std::accumulate( 
+				first_row.begin(), first_row.end(), std::string() ) ) ;
+		return wrap(res) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	
+	m <- matrix( letters, ncol = 2 )
+	checkEquals( funx(m), paste( m[1,], collapse = "" ), msg = "CharacterVector::Row" )
+}
+
+test.List.row <- function(){
+	
+	funx <- cfunction(signature(x = "matrix" ), '
+		List m(x) ;
+		List::Row first_row = m.row(0) ;
+		IntegerVector out( first_row.size() ) ;
+		std::transform( 
+			first_row.begin(), first_row.end(),
+			out.begin(), 
+			unary_call<SEXP,int>( Function("length" ) ) ) ;
+		return wrap(out) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	
+	m <- lapply( 1:16, function(i) seq(from=1, to = i ) )
+	dim( m ) <- c( 4, 4 )
+	checkEquals( funx( m ), 1 + 0:3*4, msg = "List::Row" )
+	
+}
+

Modified: pkg/Rcpp/src/Rcpp/CharacterVector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/CharacterVector.h	2010-02-23 12:51:06 UTC (rev 778)
+++ pkg/Rcpp/src/Rcpp/CharacterVector.h	2010-02-24 10:36:46 UTC (rev 779)
@@ -24,6 +24,7 @@
 
 #include <RcppCommon.h>
 #include <Rcpp/VectorBase.h>
+#include <Rcpp/MatrixRow.h>
 #include <Rcpp/Dimension.h>
 #include <Rcpp/r_cast.h>
 
@@ -148,6 +149,8 @@
 	};
 	
 	typedef StringProxy value_type ;
+	typedef MatrixRow<CharacterVector> Row ;
+	typedef StringProxy reference ;
 	
 	/**
 	 * Default constructor. Sets the underlying object to NULL
@@ -280,6 +283,10 @@
 		if( update ) setSEXP(x) ;
 	}
 
+	inline Row row( int i){
+		return Row(*this, i ) ;
+	}
+	
 } ;
 
 typedef CharacterVector StringVector ;

Added: pkg/Rcpp/src/Rcpp/MatrixRow.h
===================================================================
--- pkg/Rcpp/src/Rcpp/MatrixRow.h	                        (rev 0)
+++ pkg/Rcpp/src/Rcpp/MatrixRow.h	2010-02-24 10:36:46 UTC (rev 779)
@@ -0,0 +1,112 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// MatrixRow.h: Rcpp R/C++ interface class library -- rows of matrices
+//
+// 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_Row_h
+#define Rcpp_Row_h
+
+#include <RcppCommon.h>
+#include <Rcpp/VectorBase.h>
+
+namespace Rcpp{
+
+template <typename VECTOR>
+class MatrixRow {
+public:
+
+	typedef typename VECTOR::reference reference ;
+	typedef typename VECTOR::value_type value_type ;
+	
+	class iterator {
+	public:
+		typedef int difference_type ;
+		typedef typename VECTOR::reference reference ;
+		typedef typename VECTOR::value_type value_type ;
+		typedef value_type* pointer ;
+	
+		typedef std::random_access_iterator_tag iterator_category ;
+		
+		iterator( MatrixRow& row_, int index_ ) : row(row_), index(index_){}
+		
+		iterator& operator++(){ index++; }
+		iterator& operator++(int) { index++; }
+	
+		iterator& operator--(){ index-- ; } 
+		iterator& operator--(int){ index-- ;}
+		                    
+		iterator operator+(difference_type n) const { return iterator( row, index + n ) ; }
+		iterator operator-(difference_type n) const { return iterator( row, index - n ) ; }
+		
+		iterator& operator+=(difference_type n) { index += n ;} 
+		iterator& operator-=(difference_type n) { index -= n ;} 
+        	
+		reference operator*() {
+			return row[index] ;
+		}
+		pointer operator->(){
+			return &row[index] ;
+		}
+		
+		bool operator==( const iterator& other) { return index == other.index ; } 
+		bool operator!=( const iterator& other) { return index != other.index ; }
+		bool operator<( const iterator& other ) { return index < other.index ;}
+		bool operator>( const iterator& other ) { return index > other.index ;}
+		bool operator<=( const iterator& other ) { return index <= other.index ; }
+		bool operator>=( const iterator& other ) { return index >= other.index ; }
+		
+		difference_type operator-(const iterator& other) {
+			return index - other.index ;
+		}
+		
+	private:
+		MatrixRow& row ;
+		int index ;
+	} ;
+	
+	MatrixRow( VECTOR& object, int i ) : parent(object), index(i){
+		if( ! ::Rf_isMatrix(parent) ) throw VectorBase::not_a_matrix() ;
+		if( i < 0 || i >= parent.nrow() ) throw RObject::index_out_of_bounds() ;
+	}
+	
+	reference operator[]( const int& i ){
+		/* TODO: should we cache nrow and ncol */
+		return parent[ index + i * parent.nrow() ] ;
+	}
+	
+	inline iterator begin(){
+		return iterator( *this, 0 ) ;
+	}
+	
+	inline iterator end(){
+		return iterator( *this, size() ) ;
+	}
+	
+	inline int size(){
+		return parent.ncol() ;
+	}
+	
+private:
+	VECTOR& parent; 
+	int index ;
+} ;
+	
+	
+} // namespace Rcpp
+#endif

Modified: pkg/Rcpp/src/Rcpp/SEXP_Vector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/SEXP_Vector.h	2010-02-23 12:51:06 UTC (rev 778)
+++ pkg/Rcpp/src/Rcpp/SEXP_Vector.h	2010-02-24 10:36:46 UTC (rev 779)
@@ -24,6 +24,7 @@
 
 #include <RcppCommon.h>
 #include <Rcpp/VectorBase.h>
+#include <Rcpp/MatrixRow.h>
 #include <Rcpp/Environment.h>
 #include <Rcpp/Dimension.h>
 
@@ -31,7 +32,6 @@
 
 class SEXP_Vector_Base : public VectorBase {
 public:
-	
 	class iterator ;
 	class NameProxy ;
 	
@@ -115,6 +115,9 @@
 template <int RTYPE>
 class SEXP_Vector : public SEXP_Vector_Base{
 public:
+	typedef MatrixRow<SEXP_Vector> Row ;
+	typedef Proxy reference ;
+	typedef Proxy value_type ;
 	
 	class NameProxy {
 	public:
@@ -281,6 +284,8 @@
 		return iterator( *this, first ) ;
 	}
 	
+	inline Row row( int i){ return Row(*this, i ) ; }
+	
 private:
 	
 	/* 

Modified: pkg/Rcpp/src/Rcpp/SimpleVector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/SimpleVector.h	2010-02-23 12:51:06 UTC (rev 778)
+++ pkg/Rcpp/src/Rcpp/SimpleVector.h	2010-02-24 10:36:46 UTC (rev 779)
@@ -23,7 +23,7 @@
 #define Rcpp_SimpleVector_h
 
 #include <RcppCommon.h>
-
+#include <Rcpp/MatrixRow.h>
 #include <Rcpp/VectorBase.h>
 #include <Rcpp/r_cast.h>
 #include <Rcpp/Dimension.h>
@@ -37,82 +37,8 @@
 	typedef typename traits::storage_type<RTYPE>::type value_type ;
 	typedef value_type* iterator ;
 	typedef value_type& reference ;
+	typedef MatrixRow<SimpleVector> Row ;
 	
-	class Row {
-	public:
-		class RowIterator {
-		public:
-			typedef typename traits::storage_type<RTYPE>::type value_type ;
-			typedef value_type& reference ;
-			typedef value_type* pointer ;
-			typedef int difference_type ;
-			typedef std::random_access_iterator_tag iterator_category ;
-			
-			RowIterator( Row& row_, int index_ ) : row(row_), index(index_){}
-			
-			RowIterator& operator++(){ index++; }
-			RowIterator& operator++(int) { index++; }
-		
-			RowIterator& operator--(){ index-- ; } 
-			RowIterator& operator--(int){ index-- ;}
-			                    
-			RowIterator operator+(difference_type n) const { return iterator( row, index + n ) ; }
-			RowIterator operator-(difference_type n) const { return iterator( row, index - n ) ; }
-			
-			RowIterator& operator+=(difference_type n) { index += n ;} 
-			RowIterator& operator-=(difference_type n) { index -= n ;} 
-                	
-			reference operator*() {
-				return row[index] ;
-			}
-			pointer operator->(){
-				return &row[index] ;
-			}
-			
-			bool operator==( const RowIterator& other) { return index == other.index ; } 
-			bool operator!=( const RowIterator& other) { return index != other.index ; }
-			bool operator<( const RowIterator& other ) { return index < other.index ;}
-			bool operator>( const RowIterator& other ) { return index > other.index ;}
-			bool operator<=( const RowIterator& other ) { return index <= other.index ; }
-			bool operator>=( const RowIterator& other ) { return index >= other.index ; }
-			
-			difference_type operator-(const RowIterator& other) {
-				return index - other.index ;
-			}
-			
-		private:
-			Row& row ;
-			int index ;
-		} ;
-		
-		typedef typename traits::storage_type<RTYPE>::type value_type ;
-		
-		Row( SimpleVector& object, int i ) : parent(object), index(i){
-			if( ! ::Rf_isMatrix(parent) ) throw VectorBase::not_a_matrix() ;
-		}
-		
-		value_type& operator[]( const int& i ){
-			return parent[ index + i * parent.nrow() ] ;
-		}
-		
-		inline RowIterator begin(){
-			return RowIterator( *this, 0 ) ;
-		}
-		
-		inline RowIterator end(){
-			return RowIterator( *this, size() ) ;
-		}
-		
-		inline int size(){
-			return parent.ncol() ;
-		}
-		
-	private:
-		SimpleVector& parent; 
-		int index ;
-	} ;
-	
-	
 	SimpleVector() : VectorBase(), start(0){}
 	
 	SimpleVector(SEXP x) throw(RObject::not_compatible) : VectorBase(), start(0){
@@ -184,17 +110,11 @@
 		setSEXP( x) ;
 		UNPROTECT(1) ;
 	}
-
-	inline int ncol(){
-		if( !::Rf_isMatrix(m_sexp) ) throw VectorBase::not_a_matrix() ;
-		return dims()[1]; 
-	}
 	
-	inline int nrow(){
-		if( !::Rf_isMatrix(m_sexp) ) throw VectorBase::not_a_matrix() ;
-		return dims()[0]; 
+	inline Row row( int i ){
+		return Row( *this, i ) ;
 	}
-	
+
 protected:
 	void init(){
 		internal::r_init_vector<RTYPE>(m_sexp) ;
@@ -211,10 +131,6 @@
 		update_vector() ;
 	}
 	
-	inline int* dims(){
-		return INTEGER( ::Rf_getAttrib( m_sexp, ::Rf_install( "dim") ) ) ;
-	}
-	
 } ;
 
 template <int RTYPE> 

Modified: pkg/Rcpp/src/Rcpp/VectorBase.h
===================================================================
--- pkg/Rcpp/src/Rcpp/VectorBase.h	2010-02-23 12:51:06 UTC (rev 778)
+++ pkg/Rcpp/src/Rcpp/VectorBase.h	2010-02-24 10:36:46 UTC (rev 779)
@@ -50,6 +50,14 @@
      */
     inline R_len_t size() const { return ::Rf_length( m_sexp ) ; }
 
+    	inline int ncol(){
+		return dims()[1]; 
+	}
+	
+	inline int nrow(){
+		return dims()[0]; 
+	}
+	
     /**
      * offset based on the dimensions of this vector
      */
@@ -90,6 +98,14 @@
 	} ;
     	
     NamesProxy names() const ;
+    
+private:
+		
+	inline int* dims(){
+		if( !::Rf_isMatrix(m_sexp) ) throw VectorBase::not_a_matrix() ;
+		return INTEGER( ::Rf_getAttrib( m_sexp, ::Rf_install( "dim") ) ) ;
+	}
+	
 } ;
 
 } // namespace

Modified: pkg/Rcpp/src/Rcpp.h
===================================================================
--- pkg/Rcpp/src/Rcpp.h	2010-02-23 12:51:06 UTC (rev 778)
+++ pkg/Rcpp/src/Rcpp.h	2010-02-24 10:36:46 UTC (rev 779)
@@ -53,6 +53,7 @@
 #include <Rcpp/grow.h>
 #include <Rcpp/Dimension.h>
 #include <Rcpp/VectorBase.h>
+#include <Rcpp/MatrixRow.h>
 #include <Rcpp/SimpleVector.h>
 #include <Rcpp/SEXP_Vector.h>
 #include <Rcpp/XPtr.h>



More information about the Rcpp-commits mailing list