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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Feb 24 12:27:03 CET 2010


Author: romain
Date: 2010-02-24 12:27:03 +0100 (Wed, 24 Feb 2010)
New Revision: 781

Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/unitTests/runit.Column.R
   pkg/Rcpp/inst/unitTests/runit.Row.R
   pkg/Rcpp/src/Rcpp/MatrixColumn.h
   pkg/Rcpp/src/Rcpp/MatrixRow.h
Log:
minor changes in MatrixColumn and MatrixRow

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-02-24 10:53:33 UTC (rev 780)
+++ pkg/Rcpp/inst/ChangeLog	2010-02-24 11:27:03 UTC (rev 781)
@@ -12,6 +12,9 @@
 	* src/Rcpp/SEXP_Vector.h : gains a row method that returns 
 	a SEXP_Vector::Row aka MatrixRow<SEXP_Vector>
 
+	* src/Rcpp/MatrixColumn.h : Column class to access elements of a 
+	matrix column
+
 2010-02-23  Romain Francois <romain at r-enthusiasts.com>
 
 	* src/Rcpp/SimpleVector.h: added the class SimpleVector::Row

Modified: pkg/Rcpp/inst/unitTests/runit.Column.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Column.R	2010-02-24 10:53:33 UTC (rev 780)
+++ pkg/Rcpp/inst/unitTests/runit.Column.R	2010-02-24 11:27:03 UTC (rev 781)
@@ -27,6 +27,19 @@
 	checkEquals( funx( x ), sum( x[,1], msg = "iterating over a column" )
 }
 
+test.NumericMatrix.column.operator.equal <- function(){
+	funx <- cfunction(signature(x = "matrix" ), '
+		NumericMatrix m(x) ;
+		NumericMatrix::Column first  = m.column(0) ;
+		NumericMatrix::Column second = m.column(1) ;
+		first = second ;
+		return R_NilValue ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	x <- matrix( 1:16 + .5, ncol = 4 )
+	funx( x )
+	checkEquals( x[,1], x[,2], msg = "column(0) = column(1) " )
+}
+
 test.CharacterMatrix.column <- function(){
 	funx <- cfunction(signature(x = "matrix" ), '
 		CharacterVector m(x) ;
@@ -60,3 +73,4 @@
 	
 }
 
+

Modified: pkg/Rcpp/inst/unitTests/runit.Row.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Row.R	2010-02-24 10:53:33 UTC (rev 780)
+++ pkg/Rcpp/inst/unitTests/runit.Row.R	2010-02-24 11:27:03 UTC (rev 781)
@@ -24,9 +24,23 @@
 		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" )
+	checkEquals( funx( x ), sum( x[1,] ), msg = "iterating over a row" )
 }
 
+test.NumericMatrix.row.operator.equal <- function(){
+	funx <- cfunction(signature(x = "matrix" ), '
+		NumericMatrix m(x) ;
+		NumericMatrix::Row first  = m.row(0) ;
+		NumericMatrix::Row second = m.row(1) ;
+		first = second ;
+		return R_NilValue ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	x <- matrix( 1:16 + .5, ncol = 4 )
+	funx( x )
+	checkEquals( x[1,], x[2,], msg = "row(0) = row(1)" )
+}
+
+
 test.CharacterMatrix.row <- function(){
 	funx <- cfunction(signature(x = "matrix" ), '
 		CharacterVector m(x) ;

Modified: pkg/Rcpp/src/Rcpp/MatrixColumn.h
===================================================================
--- pkg/Rcpp/src/Rcpp/MatrixColumn.h	2010-02-24 10:53:33 UTC (rev 780)
+++ pkg/Rcpp/src/Rcpp/MatrixColumn.h	2010-02-24 11:27:03 UTC (rev 781)
@@ -40,6 +40,14 @@
 		if( i < 0 || i >= parent.ncol() ) throw RObject::index_out_of_bounds() ;
 	}
 	
+	MatrixColumn( const MatrixColumn& other ) : parent(other.parent), index(other.index){} ;
+	
+	MatrixColumn& operator=( MatrixColumn& other ){
+		if( other.size() != size() ) throw std::range_error( "not compatible" ) ;
+		std::copy( other.begin(), other.end(), begin() ) ;
+		return *this ;
+	}
+	
 	reference operator[]( const int& i ){
 		/* TODO: should we cache nrow and ncol */
 		return parent[ index * parent.ncol() + i ] ;
@@ -53,7 +61,7 @@
 		return iterator( parent, index * parent.ncol() + parent.nrow() ) ;
 	}
 	
-	inline int size(){
+	inline int size() const {
 		return parent.nrow() ;
 	}
 	

Modified: pkg/Rcpp/src/Rcpp/MatrixRow.h
===================================================================
--- pkg/Rcpp/src/Rcpp/MatrixRow.h	2010-02-24 10:53:33 UTC (rev 780)
+++ pkg/Rcpp/src/Rcpp/MatrixRow.h	2010-02-24 11:27:03 UTC (rev 781)
@@ -85,6 +85,14 @@
 		if( i < 0 || i >= parent.nrow() ) throw RObject::index_out_of_bounds() ;
 	}
 	
+	MatrixRow( const MatrixRow& other ) : parent(other.parent), index(other.index){} ;
+	                          
+	MatrixRow& operator=( MatrixRow& other ){
+		if( other.size() != size() ) throw std::range_error( "not compatible" ) ;
+		std::copy( other.begin(), other.end(), begin() ) ;
+		return *this ;
+	}
+	
 	reference operator[]( const int& i ){
 		/* TODO: should we cache nrow and ncol */
 		return parent[ index + i * parent.nrow() ] ;
@@ -98,7 +106,7 @@
 		return iterator( *this, size() ) ;
 	}
 	
-	inline int size(){
+	inline int size() const {
 		return parent.ncol() ;
 	}
 	



More information about the Rcpp-commits mailing list