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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Oct 23 17:22:44 CEST 2010


Author: romain
Date: 2010-10-23 17:22:44 +0200 (Sat, 23 Oct 2010)
New Revision: 2356

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
   pkg/Rcpp/inst/include/Rcpp/vector/MatrixRow.h
Log:
faster row indexing

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2010-10-21 21:06:20 UTC (rev 2355)
+++ pkg/Rcpp/ChangeLog	2010-10-23 15:22:44 UTC (rev 2356)
@@ -1,3 +1,7 @@
+2010-10-23  Romain Francois <romain at r-enthusiasts.com>
+
+    * inst/include/Rcpp/vector/MatrixRow.h: Faster row indexing
+    
 2010-10-21  Romain Francois <romain at r-enthusiasts.com>
 
     * inst/include/Rcpp/vector/MatrixColumn.h: Fixed indexing bug (mismatch 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2010-10-21 21:06:20 UTC (rev 2355)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2010-10-23 15:22:44 UTC (rev 2356)
@@ -121,9 +121,6 @@
     inline Proxy operator[]( int i ){
     	return static_cast< Vector<RTYPE>* >( this )->operator[]( i ) ;
     }
-    // inline Proxy operator()( int i, int j ){
-    // 	return static_cast< Vector<RTYPE>* >( this )->operator()( i, j ) ;
-    // }
     
     inline Proxy operator[]( int i ) const {
     	return static_cast< const Vector<RTYPE>* >( this )->operator[]( i ) ;

Modified: pkg/Rcpp/inst/include/Rcpp/vector/MatrixRow.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/MatrixRow.h	2010-10-21 21:06:20 UTC (rev 2355)
+++ pkg/Rcpp/inst/include/Rcpp/vector/MatrixRow.h	2010-10-23 15:22:44 UTC (rev 2356)
@@ -94,26 +94,52 @@
 		int index ;
 	} ;
 	
-	MatrixRow( MATRIX& object, int i ) : parent(object), index(i){
+	MatrixRow( MATRIX& object, int i ) : 
+	    parent(object), 
+	    start(parent.begin() + i), 
+	    parent_nrow(parent.nrow()) 
+	{
 		if( i < 0 || i >= parent.nrow() ) throw index_out_of_bounds() ;
 	}
 	
-	MatrixRow( const MatrixRow& other ) : parent(other.parent), index(other.index){} ;
-	                          
-	MatrixRow& operator=( MatrixRow& other ){
-		parent = other.parent ;
-		index = other.index ;
-		return *this ;
+	MatrixRow( const MatrixRow& other ) : 
+	    parent(other.parent), 
+	    start(other.start), 
+	    parent_nrow(other.parent_nrow)
+	{} ;
+	
+	template <int RT, bool NA, typename T>
+	MatrixRow& operator=( const Rcpp::VectorBase<RT,NA,T>& rhs ){
+	    int n = size() ;
+	    const T& ref = rhs.get_ref() ;
+	    
+	    int __trip_count = n >> 2 ;
+        int i = 0 ;
+        for ( ; __trip_count > 0 ; --__trip_count) { 
+        	start[get_parent_index(i)] = ref[i] ; i++ ;            
+        	start[get_parent_index(i)] = ref[i] ; i++ ;            
+        	start[get_parent_index(i)] = ref[i] ; i++ ;            
+        	start[get_parent_index(i)] = ref[i] ; i++ ;            
+        }                                            
+        switch (n - i){                          
+          case 3:                                    
+              start[get_parent_index(i)] = ref[i] ; i++ ;             
+          case 2:                                    
+              start[get_parent_index(i)] = ref[i] ; i++ ;             
+          case 1:                                    
+              start[get_parent_index(i)] = ref[i] ; i++ ;             
+          case 0:                                    
+          default:                                   
+              {}                         
+        }
 	}
-	
+
 	reference operator[]( int i ){
-		/* TODO: should we cache nrow and ncol */
-		return parent[ get_parent_index(i) ] ;
+		return start[ get_parent_index(i) ] ;
 	}
 	
 	reference operator[]( int i ) const {
-		/* TODO: should we cache nrow and ncol */
-		return parent[ get_parent_index(i) ] ;
+		return start[ get_parent_index(i) ] ;
 	}
 	
 	inline iterator begin(){
@@ -138,9 +164,10 @@
 	
 private:
 	MATRIX& parent; 
-	int index ;
+	typename MATRIX::iterator start ;
+	int parent_nrow ;
 	
-	inline int get_parent_index(int i) const { return index + i * parent.nrow() ; }
+	inline int get_parent_index(int i) const { return i * parent_nrow ; } 
 } ;
 
 #endif



More information about the Rcpp-commits mailing list