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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Oct 21 21:10:38 CEST 2010


Author: romain
Date: 2010-10-21 21:10:37 +0200 (Thu, 21 Oct 2010)
New Revision: 2353

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/vector/MatrixColumn.h
Log:
faster column indexing

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2010-10-21 18:54:10 UTC (rev 2352)
+++ pkg/Rcpp/ChangeLog	2010-10-21 19:10:37 UTC (rev 2353)
@@ -7,7 +7,8 @@
     R-help/Rcpp-devel thread: http://article.gmane.org/gmane.comp.lang.r.rcpp/851
     
     * inst/include/Rcpp/vector/MatrixColumn.h: Column gains a operator= taking a 
-    sugar expression
+    sugar expression. Faster indexing (caching the pointer instead of calling
+    nrow many times)
     
 2010-10-21  Dirk Eddelbuettel  <edd at debian.org>
 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/MatrixColumn.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/MatrixColumn.h	2010-10-21 18:54:10 UTC (rev 2352)
+++ pkg/Rcpp/inst/include/Rcpp/vector/MatrixColumn.h	2010-10-21 19:10:37 UTC (rev 2353)
@@ -30,20 +30,17 @@
 	typedef typename MATRIX::value_type value_type ;
 	typedef typename MATRIX::iterator iterator ;
 	
-	MatrixColumn( MATRIX& object, int i ) : parent(object), index(i){
+	MatrixColumn( MATRIX& object, int i ) : 
+	    parent(object), index(i), start(parent.begin())
+	{
 		if( i < 0 || i >= parent.ncol() ) throw index_out_of_bounds() ;
 	}
 	
-	MatrixColumn( const MatrixColumn& other ) : parent(other.parent), index(other.index){} ;
+	MatrixColumn( const MatrixColumn& other ) : 
+	    parent(other.parent), index(other.index), start(other.start){} ;
 	
-	MatrixColumn& operator=( MatrixColumn& other ){
-		parent = other.parent ;
-		index  = other.index ;
-	}
-
 	template <int RT, bool NA, typename T>
 	MatrixColumn& operator=( const Rcpp::VectorBase<RT,NA,T>& rhs ){
-	    iterator start = begin() ;
 	    int n = size() ;
 	    const T& ref = rhs.get_ref() ;
 	    
@@ -70,28 +67,28 @@
 
 	Proxy operator[]( int i ){
 		/* TODO: should we cache nrow and ncol */
-		return parent[ get_parent_index(i) ] ;
+		return start[i] ;
 	}
 	
 	Proxy operator[]( int i ) const {
 		/* TODO: should we cache nrow and ncol */
-		return parent[ get_parent_index(i) ] ;
+		return start[i] ;
 	}
 	
 	inline iterator begin(){
-		return parent.begin() + index * parent.nrow() ;
+		return start ;
 	}
 	
 	inline iterator begin() const {
-		return parent.begin() + index * parent.nrow() ;
+		return start ;
 	}
 	
 	inline iterator end(){
-		return begin() + parent.nrow() ;
+		return start + parent.nrow() ;
 	}
 	
 	inline iterator end() const {
-		return begin() + parent.nrow() ;
+		return start + parent.nrow() ;
 	}
 	
 	inline int size() const {
@@ -100,9 +97,9 @@
 	
 private:
 	MATRIX& parent; 
-	int index ;
-	
-	inline int get_parent_index(int i) const { return index * parent.nrow() + i ; }
+	int index ;     
+	iterator start ;
+
 } ;
 
 #endif



More information about the Rcpp-commits mailing list