[Rcpp-commits] r2249 - in pkg/Rcpp/inst: . include/Rcpp/internal include/Rcpp/vector unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Sep 30 08:28:52 CEST 2010


Author: romain
Date: 2010-09-30 08:28:52 +0200 (Thu, 30 Sep 2010)
New Revision: 2249

Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/internal/wrap.h
   pkg/Rcpp/inst/include/Rcpp/vector/MatrixColumn.h
   pkg/Rcpp/inst/include/Rcpp/vector/MatrixRow.h
   pkg/Rcpp/inst/unitTests/runit.Matrix.R
Log:
Row and Column become sugar expressions

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-09-29 17:29:20 UTC (rev 2248)
+++ pkg/Rcpp/inst/ChangeLog	2010-09-30 06:28:52 UTC (rev 2249)
@@ -1,3 +1,13 @@
+2010-09-30  Romain Francois <romain at r-enthusiasts.com>
+
+        * inst/include/Rcpp/vector/MatrixRow.h: Matrix row inherits 
+        VectorBase so that it becomes a sugar expression, so that we can do 
+        something like this: 
+        NumericMatrix x( ... ) ; 
+        NumericVector x0 = x.row(0) ;
+        
+        * inst/include/Rcpp/vector/MatrixColumn.h: idem for columns
+
 2010-09-27  Romain Francois <romain at r-enthusiasts.com>
 
         * inst/include/Rcpp/traits/is_sugar_expression.h: new trait class

Modified: pkg/Rcpp/inst/include/Rcpp/internal/wrap.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/internal/wrap.h	2010-09-29 17:29:20 UTC (rev 2248)
+++ pkg/Rcpp/inst/include/Rcpp/internal/wrap.h	2010-09-30 06:28:52 UTC (rev 2249)
@@ -317,6 +317,7 @@
  */
 template<typename InputIterator, typename T>
 inline SEXP range_wrap_dispatch( InputIterator first, InputIterator last ){
+	RCPP_DEBUG_2( "range_wrap_dispatch< InputIterator = \n%s , T = %s>\n", DEMANGLE(InputIterator), DEMANGLE(T) ) ;
 	return range_wrap_dispatch___impl<InputIterator,T>( first, last, typename ::Rcpp::traits::r_type_traits<T>::r_category() ) ;
 }
 
@@ -327,7 +328,7 @@
  */
 template <typename InputIterator>
 inline SEXP range_wrap(InputIterator first, InputIterator last){
-	return range_wrap_dispatch<InputIterator,typename std::iterator_traits<InputIterator>::value_type>( first, last ) ;
+	return range_wrap_dispatch<InputIterator,typename traits::remove_reference<typename std::iterator_traits<InputIterator>::value_type>::type >( first, last ) ;
 }
 // }}}
 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/MatrixColumn.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/MatrixColumn.h	2010-09-29 17:29:20 UTC (rev 2248)
+++ pkg/Rcpp/inst/include/Rcpp/vector/MatrixColumn.h	2010-09-30 06:28:52 UTC (rev 2249)
@@ -22,9 +22,8 @@
 #ifndef Rcpp__vector__MatrixColumn_h
 #define Rcpp__vector__MatrixColumn_h
    
-
 template <int RTYPE>
-class MatrixColumn {
+class MatrixColumn : public VectorBase<RTYPE,true,MatrixColumn<RTYPE> > {
 public:
 	typedef Matrix<RTYPE> MATRIX ;
 	typedef typename MATRIX::Proxy Proxy ;
@@ -42,19 +41,32 @@
 		index  = other.index ;
 	}
 	
-	Proxy operator[]( const int& i ){
+	Proxy operator[]( int i ){
 		/* TODO: should we cache nrow and ncol */
-		return parent[ index * parent.ncol() + i ] ;
+		return parent[ get_parent_index(i) ] ;
 	}
 	
-	iterator begin(){
+	Proxy operator[]( int i ) const {
+		/* TODO: should we cache nrow and ncol */
+		return parent[ get_parent_index(i) ] ;
+	}
+	
+	inline iterator begin(){
 		return parent.begin() + index * parent.ncol() ;
 	}
 	
-	iterator end(){
-		return parent.begin() + index * parent.ncol() + parent.nrow() ;
+	inline iterator begin() const {
+		return parent.begin() + index * parent.ncol() ;
 	}
 	
+	inline iterator end(){
+		return begin() + parent.nrow() ;
+	}
+	
+	inline iterator end() const {
+		return begin() + parent.nrow() ;
+	}
+	
 	inline int size() const {
 		return parent.nrow() ;
 	}
@@ -62,6 +74,8 @@
 private:
 	MATRIX& parent; 
 	int index ;
+	
+	inline int get_parent_index(int i) const { return index * parent.ncol() + i ; }
 } ;
 
 #endif

Modified: pkg/Rcpp/inst/include/Rcpp/vector/MatrixRow.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/MatrixRow.h	2010-09-29 17:29:20 UTC (rev 2248)
+++ pkg/Rcpp/inst/include/Rcpp/vector/MatrixRow.h	2010-09-30 06:28:52 UTC (rev 2249)
@@ -23,11 +23,12 @@
 #define Rcpp__vector__MatrixRow_h
    
 template <int RTYPE>
-class MatrixRow {
+class MatrixRow : public VectorBase< RTYPE, true, MatrixRow<RTYPE> > {
 public:
 	typedef Matrix<RTYPE> MATRIX ;
 	typedef typename MATRIX::Proxy Proxy ;
 	typedef typename MATRIX::Proxy reference ;
+	typedef typename MATRIX::value_type value_type ;
 	
 	class iterator {
 	public:
@@ -80,6 +81,10 @@
 		bool operator<=( const iterator& other ) { return index <= other.index ; }
 		bool operator>=( const iterator& other ) { return index >= other.index ; }
 		
+		inline reference operator[]( int i){
+		    return row[ index + i ] ;
+		}
+		
 		difference_type operator-(const iterator& other) {
 			return index - other.index ;
 		}
@@ -101,11 +106,16 @@
 		return *this ;
 	}
 	
-	reference operator[]( const int& i ){
+	reference operator[]( int i ){
 		/* TODO: should we cache nrow and ncol */
-		return parent[ index + i * parent.nrow() ] ;
+		return parent[ get_parent_index(i) ] ;
 	}
 	
+	reference operator[]( int i ) const {
+		/* TODO: should we cache nrow and ncol */
+		return parent[ get_parent_index(i) ] ;
+	}
+	
 	inline iterator begin(){
 		return iterator( *this, 0 ) ;
 	}
@@ -114,6 +124,14 @@
 		return iterator( *this, size() ) ;
 	}
 	
+	inline iterator begin() const {
+		return iterator( const_cast<MatrixRow&>(*this), 0 ) ;
+	}
+	
+	inline iterator end() const {
+		return iterator( const_cast<MatrixRow&>(*this), size() ) ;
+	}
+	
 	inline int size() const {
 		return parent.ncol() ;
 	}
@@ -121,6 +139,8 @@
 private:
 	MATRIX& parent; 
 	int index ;
+	
+	inline int get_parent_index(int i) const { return index + i * parent.nrow() ; }
 } ;
 
 #endif

Modified: pkg/Rcpp/inst/unitTests/runit.Matrix.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Matrix.R	2010-09-29 17:29:20 UTC (rev 2248)
+++ pkg/Rcpp/inst/unitTests/runit.Matrix.R	2010-09-30 06:28:52 UTC (rev 2249)
@@ -154,6 +154,21 @@
 						unary_call<SEXP,int>( Function("length" ) ) ) ;
 					return wrap(out) ;
 				' 			
+			), 
+			"runit_Row_Column_sugar" = list( 
+			    signature( x_ = "matrix" ), 
+			    '
+			    NumericMatrix x( x_) ;
+			    NumericVector r0 = x.row(0) ;
+			    NumericVector c0 = x.column(0) ;
+			    return List::create( 
+			        r0, 
+			        c0, 
+			        x.row(1), 
+			        x.column(1), 
+			        x.row(1) + x.column(1)
+			        ) ;
+			    '
 			)
 
         )
@@ -271,3 +286,19 @@
 	
 }
 
+test.List.column <- function(){
+	funx <- .rcpp.Matrix$runit_Row_Column_sugar
+	x <- matrix( 1:16+.5, nc = 4 )
+	res <- funx( x )
+	target <- list( 
+	    x[1,], 
+	    x[,1], 
+	    x[2,],
+	    x[,2], 
+	    x[2,] + x[,2]
+	    )
+	checkEquals( res, target, msg = "colum and row as sugar" )
+	
+}
+
+



More information about the Rcpp-commits mailing list