[Rcpp-commits] r1907 - in pkg/Rcpp: . inst inst/include/Rcpp/sugar/matrix inst/include/Rcpp/vector

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Aug 4 09:38:41 CEST 2010


Author: romain
Date: 2010-08-04 09:38:40 +0200 (Wed, 04 Aug 2010)
New Revision: 1907

Modified:
   pkg/Rcpp/DESCRIPTION
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/col.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/diag.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/lower_tri.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/matrix_functions.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/outer.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/row.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/upper_tri.h
   pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
   pkg/Rcpp/inst/include/Rcpp/vector/MatrixBase.h
Log:
rework sugar matrix so that it uses the more efficient operator()(int,int) since many (all) sugar matrix expressions are better expressed with both index

Modified: pkg/Rcpp/DESCRIPTION
===================================================================
--- pkg/Rcpp/DESCRIPTION	2010-08-03 19:07:17 UTC (rev 1906)
+++ pkg/Rcpp/DESCRIPTION	2010-08-04 07:38:40 UTC (rev 1907)
@@ -1,6 +1,6 @@
 Package: Rcpp
 Title: Seamless R and C++ Integration
-Version: 0.8.5.3
+Version: 0.8.5.4
 Date: $Date$
 Author: Dirk Eddelbuettel and Romain Francois, with contributions 
  by Simon Urbanek, David Reiss and Douglas Bates; based on code written during 

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-08-03 19:07:17 UTC (rev 1906)
+++ pkg/Rcpp/inst/ChangeLog	2010-08-04 07:38:40 UTC (rev 1907)
@@ -1,3 +1,8 @@
+2010-08-04  Romain Francois <romain at r-enthusiasts.com>
+
+	* inst/include/Rcpp/sugar/: rework sugar matrix so that operator()(int,int)
+	is always used instead of operator[](int)
+
 2010-08-02  Romain Francois <romain at r-enthusiasts.com>
 
 	* inst/include/Rcpp/XPtr.h: give default values to tag and prot in XPtr ctor

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/col.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/col.h	2010-08-03 19:07:17 UTC (rev 1906)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/col.h	2010-08-04 07:38:40 UTC (rev 1907)
@@ -36,9 +36,9 @@
 	
 	Col( const LHS_TYPE& lhs) : nr( lhs.ncol() ), nc( lhs.ncol() ) {}
 	
-	inline int operator[]( int index ) const {
-		return Rcpp::internal::get_column( index, nr) + 1 ;
-	}
+	// inline int operator[]( int index ) const {
+	// 	return Rcpp::internal::get_column( index, nr) + 1 ;
+	// }
 	inline int operator()( int i, int j ) const {
 		return j + 1 ;
 	}

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/diag.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/diag.h	2010-08-03 19:07:17 UTC (rev 1906)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/diag.h	2010-08-04 07:38:40 UTC (rev 1907)
@@ -56,11 +56,11 @@
 	
 	Diag_Maker( const VEC_TYPE& object_ ) : object(object_), n(object_.size()) {}
 	
-	inline STORAGE operator[]( int index ) const {
-		int i = Rcpp::internal::get_line( index, n ) ;
-		int j = Rcpp::internal::get_column( index, n, i ) ;
-		return (i==j) ? object[i] : 0 ;
-	}
+	// inline STORAGE operator[]( int index ) const {
+	// 	int i = Rcpp::internal::get_line( index, n ) ;
+	// 	int j = Rcpp::internal::get_column( index, n, i ) ;
+	// 	return (i==j) ? object[i] : 0 ;
+	// }
 	inline STORAGE operator()( int i, int j ) const {
 		return (i==j) ? object[i] : 0 ;
 	}

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/lower_tri.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/lower_tri.h	2010-08-03 19:07:17 UTC (rev 1906)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/lower_tri.h	2010-08-04 07:38:40 UTC (rev 1907)
@@ -38,11 +38,11 @@
 		nr( lhs.nrow() ), nc( lhs.ncol() ), 
 		getter( diag ? (&LowerTri::get_diag_true) : (&LowerTri::get_diag_false) ){}
 	
-	inline int operator[]( int index ) const {
-		int i = Rcpp::internal::get_line( index, nr ) ;
-		int j = Rcpp::internal::get_column( index, nr, i ) ;
-		return get(i,j) ; 
-	}
+	// inline int operator[]( int index ) const {
+	// 	int i = Rcpp::internal::get_line( index, nr ) ;
+	// 	int j = Rcpp::internal::get_column( index, nr, i ) ;
+	// 	return get(i,j) ; 
+	// }
 	inline int operator()( int i, int j ) const {
 		return get(i,j) ;
 	}

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/matrix_functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/matrix_functions.h	2010-08-03 19:07:17 UTC (rev 1906)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/matrix_functions.h	2010-08-04 07:38:40 UTC (rev 1907)
@@ -22,7 +22,6 @@
 #ifndef RCPP_SUGAR_MATRIX_FUNCTIONS_H
 #define RCPP_SUGAR_MATRIX_FUNCTIONS_H
 
-#include <Rcpp/sugar/matrix/tools.h>
 #include <Rcpp/sugar/matrix/outer.h>
 #include <Rcpp/sugar/matrix/row.h>
 #include <Rcpp/sugar/matrix/col.h>

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/outer.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/outer.h	2010-08-03 19:07:17 UTC (rev 1906)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/outer.h	2010-08-04 07:38:40 UTC (rev 1907)
@@ -49,11 +49,12 @@
 	Outer( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_, Function fun_ ) : 
 		lhs(lhs_), rhs(rhs_), fun(fun_), nr(lhs_.size()), nc(rhs_.size()) {}
 	
-	inline STORAGE operator[]( int index ) const {
-		int i = Rcpp::internal::get_line( index, nr ) ;
-		int j = Rcpp::internal::get_column( index, nr, i ) ;
-		return converter_type::get( fun( lhs[i], rhs[j] ) );
-	}
+	// inline STORAGE operator[]( int index ) const {
+	// 	int i = Rcpp::internal::get_line( index, nr ) ;
+	// 	int j = Rcpp::internal::get_column( index, nr, i ) ;
+	// 	return converter_type::get( fun( lhs[i], rhs[j] ) );
+	// }
+	
 	inline STORAGE operator()( int i, int j ) const {
 		return converter_type::get( fun( lhs[i], rhs[j] ) );
 	}

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/row.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/row.h	2010-08-03 19:07:17 UTC (rev 1906)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/row.h	2010-08-04 07:38:40 UTC (rev 1907)
@@ -36,9 +36,9 @@
 	
 	Row( const LHS_TYPE& lhs) : nr( lhs.nrow() ), nc( lhs.ncol() ) {}
 	
-	inline int operator[]( int index ) const {
-		return Rcpp::internal::get_line( index, nr) + 1;
-	}
+	// inline int operator[]( int index ) const {
+	// 	return Rcpp::internal::get_line( index, nr) + 1;
+	// }
 	inline int operator()( int i, int j ) const {
 		return i + 1 ;
 	}

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/upper_tri.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/upper_tri.h	2010-08-03 19:07:17 UTC (rev 1906)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/upper_tri.h	2010-08-04 07:38:40 UTC (rev 1907)
@@ -38,11 +38,11 @@
 		nr( lhs.nrow() ), nc( lhs.ncol() ), 
 		getter( diag ? (&UpperTri::get_diag_true) : (&UpperTri::get_diag_false) ){}
 	
-	inline int operator[]( int index ) const {
-		int i = Rcpp::internal::get_line( index, nr ) ;
-		int j = Rcpp::internal::get_column( index, nr, i ) ;
-		return get(i,j) ; 
-	}
+	// inline int operator[]( int index ) const {
+	// 	int i = Rcpp::internal::get_line( index, nr ) ;
+	// 	int j = Rcpp::internal::get_column( index, nr, i ) ;
+	// 	return get(i,j) ; 
+	// }
 	inline int operator()( int i, int j ) const {
 		return get(i,j) ;
 	}

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2010-08-03 19:07:17 UTC (rev 1906)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2010-08-04 07:38:40 UTC (rev 1907)
@@ -82,16 +82,18 @@
     	Rf_setAttrib( x, Rf_install("dim"), d ) ;
     	RObject::setSEXP( x ) ;
     	UNPROTECT( 2 ) ;
-    	import_matrix_expression<NA,MAT>( other, nr * nc ) ;
+    	import_matrix_expression<NA,MAT>( other, nr, nc ) ;
 	}
    
 private:
 	
 	template <bool NA, typename MAT>
-    void import_matrix_expression( const MatrixBase<RTYPE,NA,MAT>& other, int n ){
-    	iterator start = VECTOR::begin() ; 
-		for( int i=0; i<n; i++, ++start){
-			*start = other[i] ;
+    void import_matrix_expression( const MatrixBase<RTYPE,NA,MAT>& other, int nr, int nc ){
+    	iterator start = VECTOR::begin() ;
+    	for( int j=0; j<nc; j++){
+			for( int i=0; i<nr; i++, ++start){
+				*start = other(i,j) ;
+			}
 		}
     }
 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/MatrixBase.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/MatrixBase.h	2010-08-03 19:07:17 UTC (rev 1906)
+++ pkg/Rcpp/inst/include/Rcpp/vector/MatrixBase.h	2010-08-04 07:38:40 UTC (rev 1907)
@@ -22,6 +22,8 @@
 #ifndef Rcpp__vector__MatrixBase_h
 #define Rcpp__vector__MatrixBase_h
 
+#include <Rcpp/sugar/matrix/tools.h>
+
 namespace Rcpp{
 	
 /** a base class for vectors, modelled after the CRTP */
@@ -37,10 +39,9 @@
 		return static_cast<MATRIX&>(*this) ;
 	}
 
-	inline stored_type operator[]( int i) const { 
-		return static_cast<const MATRIX*>(this)->operator[](i) ;
-	}
-	
+	// inline stored_type operator[]( int i) const { 
+	// 	return static_cast<const MATRIX*>(this)->operator[](i) ;
+	// }
 	inline stored_type operator()( int i, int j) const throw(not_a_matrix) {
 		return static_cast<const MATRIX*>(this)->operator()(i, j) ;
 	}
@@ -57,40 +58,62 @@
 		typedef stored_type value_type;
 		typedef std::random_access_iterator_tag iterator_category ;
 
-		iterator( const MatrixBase& object_, int index_ ) : object(object_), index(index_){} 
-		iterator( const iterator& other) : object(other.object), index(other.index){};
+		iterator( const MatrixBase& object_, int index_ ) : 
+			object(object_), i(0), j(0), nr(object_.nrow()), nc(object_.ncol()) {
+			
+			update_index( index_) ;	
+		}
 		
+		iterator( const iterator& other) : 
+			object(other.object), i(other.i), j(other.j), nr(other.nr), nc(other.nc){}
+		
 		inline iterator& operator++(){
-			index++ ;
+			i++ ;
+			if( i == nr ){
+				j++ ;
+				i=0 ;
+			}
 			return *this ;
 		}
 		inline iterator& operator++(int){
-			index++;
+			i++ ;
+			if( i == nr ){
+				j++ ;
+				i=0 ;
+			}
 			return *this ;
 		}
 		
 		inline iterator& operator--(){
-			index-- ;
+			i-- ;
+			if( i == -1 ){
+				j-- ;
+				i = nr - 1 ;
+			}
 			return *this ;
 		}
 		inline iterator& operator--(int){
-			index--; 
+			i-- ;
+			if( i == -1 ){
+				j-- ;
+				i = nr - 1 ;
+			}
 			return *this ;
 		}
 		                    
 		inline iterator operator+(difference_type n) const {
-			return iterator( object, index+n ) ;
+			return iterator( object, i + (j*nr) + n ) ;
 		}
 		inline iterator operator-(difference_type n) const {
-			return iterator( object, index-n ) ;
+			return iterator( object, i + (j*nr) - n ) ;
 		}
 		
 		inline iterator& operator+=(difference_type n) {
-			index += n ;
+			update_index( i + j*nr + n );
 			return *this ;
 		}
 		inline iterator& operator-=(difference_type n) {
-			index -= n; 
+			update_index( i + j*nr - n );
 			return *this ;
 		}
 
@@ -98,42 +121,50 @@
 			//  TODO: it might be better to call object( i, j )
 			//        as in many cases the sugar expression 
 			//        is faster with two indexes
-			return object[index] ;
+			return object(i,j) ;
 		}
 		inline pointer operator->(){
-			return &object[index] ;
+			return &object(i,j) ;
 		}
 		
 		inline bool operator==( const iterator& y) const {
-			return ( index == y.index ) ;
+			return ( i == y.i && j == y.j ) ;
 		}
 		inline bool operator!=( const iterator& y) const {
-			return ( index != y.index ) ;
+			return ( i != y.i || j != y.j ) ;
 		}
 		inline bool operator<( const iterator& other ) const {
-			return index < other.index ;
+			return index() < other.index() ;
 		}
 		inline bool operator>( const iterator& other ) const {
-			return index > other.index ;
+			return index() > other.index() ;
 		}
 		inline bool operator<=( const iterator& other ) const {
-			return index <= other.index ;
+			return index() <= other.index() ;
 		}
 		inline bool operator>=( const iterator& other ) const {
-			return index >= other.index ;
+			return index() >= other.index() ;
 		}
 		
 		inline difference_type operator-(const iterator& other) const {
-			return index - other.index ;
+			return index() - other.index() ;
 		}
 	
 			
 	private:
 		const MatrixBase& object ;
-		int index;
+		int i, j ;
+		int nr, nc ;
 		
-		// int i, j ;
-		// int nr, nc ;
+		inline void update_index( int index_ ){
+			i = internal::get_line( index_, nr );
+			j = internal::get_column( index_, nr, i ) ;
+		}
+		
+		inline int index() const {
+			return i + nr * j ;
+		}
+		
 	} ;
 	
 	inline iterator begin() const { return iterator(*this, 0) ; }



More information about the Rcpp-commits mailing list