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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Feb 23 13:51:06 CET 2010


Author: romain
Date: 2010-02-23 13:51:06 +0100 (Tue, 23 Feb 2010)
New Revision: 778

Added:
   pkg/Rcpp/inst/unitTests/runit.Row.R
Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/src/Rcpp/SimpleVector.h
Log:
+SimpleVector::Row and SimpleVector::Row::RowIterator

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-02-23 12:08:03 UTC (rev 777)
+++ pkg/Rcpp/inst/ChangeLog	2010-02-23 12:51:06 UTC (rev 778)
@@ -1,5 +1,8 @@
 2010-02-23  Romain Francois <romain at r-enthusiasts.com>
 
+	* src/Rcpp/SimpleVector.h: added the class SimpleVector::Row
+	to support STL algorithms working on a row of a matrix. 
+
 	* src/Rcpp/internal/ListInitialization.h: new class that 
 	supports use of operator, (inspired from blitz) giving this 
 	notation: IntegerVector x(3); x = 0, 1, 2 ;

Added: pkg/Rcpp/inst/unitTests/runit.Row.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Row.R	                        (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.Row.R	2010-02-23 12:51:06 UTC (rev 778)
@@ -0,0 +1,29 @@
+#!/usr/bin/r -t
+#
+# 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/>.
+
+test.NumericMatrix <- function(){
+	funx <- cfunction(signature(x = "matrix" ), '
+		NumericMatrix m(x) ;
+		NumericMatrix::Row first_row( m, 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" )
+}
+

Modified: pkg/Rcpp/src/Rcpp/SimpleVector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/SimpleVector.h	2010-02-23 12:08:03 UTC (rev 777)
+++ pkg/Rcpp/src/Rcpp/SimpleVector.h	2010-02-23 12:51:06 UTC (rev 778)
@@ -38,6 +38,81 @@
 	typedef value_type* iterator ;
 	typedef value_type& reference ;
 	
+	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){
@@ -110,6 +185,16 @@
 		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]; 
+	}
+	
 protected:
 	void init(){
 		internal::r_init_vector<RTYPE>(m_sexp) ;
@@ -126,6 +211,10 @@
 		update_vector() ;
 	}
 	
+	inline int* dims(){
+		return INTEGER( ::Rf_getAttrib( m_sexp, ::Rf_install( "dim") ) ) ;
+	}
+	
 } ;
 
 template <int RTYPE> 



More information about the Rcpp-commits mailing list