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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jan 26 10:41:59 CET 2010


Author: romain
Date: 2010-01-26 10:41:59 +0100 (Tue, 26 Jan 2010)
New Revision: 460

Modified:
   pkg/inst/ChangeLog
   pkg/inst/unitTests/runit.GenericVector.R
   pkg/src/Rcpp/SEXP_Vector.h
Log:
generic vector matrix-like indexing

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-26 09:16:34 UTC (rev 459)
+++ pkg/inst/ChangeLog	2010-01-26 09:41:59 UTC (rev 460)
@@ -17,8 +17,14 @@
 	indexing for matrices of strings
 
 	* inst/unitTests/runit.CharacterVector.R: unit test for matrix
-	indexing
+	indexing (test.CharacterVector.matrix.indexing)
 
+	* inst/Rcpp/SEXP_Vector.h: use offset to implement matrix like 
+	indexing on lists
+
+	* inst/unitTests/runit.GenericVector.R: unit test for matrix
+	indexing (test.List.matrix.indexing)
+
 2010-01-25  Romain Francois <francoisromain at free.fr>
 
 	* src/Rcpp/wrap.h: wrap is back at being a template. The 

Modified: pkg/inst/unitTests/runit.GenericVector.R
===================================================================
--- pkg/inst/unitTests/runit.GenericVector.R	2010-01-26 09:16:34 UTC (rev 459)
+++ pkg/inst/unitTests/runit.GenericVector.R	2010-01-26 09:41:59 UTC (rev 460)
@@ -66,3 +66,35 @@
 	}
 }
 
+test.List.matrix.indexing <- function(){
+	
+	funx <- cfunction(signature(x = "character" ), '
+		GenericVector m(x) ;
+		GenericVector out(4) ;
+		for( size_t i=0 ; i<4; i++){
+			out[i] = m(i,i) ;
+		}
+		return out ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	
+	# a matrix of integer vectors
+	x <- structure( lapply( 1:16, function(x) seq.int(x) ), dim = c( 4, 4) )
+	checkEquals( funx(x), diag(x), msg = "matrix indexing" )
+	
+	funx <- cfunction(signature(x = "integer" ), '
+		GenericVector m(x) ;
+		for( size_t i=0 ; i<4; i++){
+			m(i,i) = "foo" ;
+		}
+		return m ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	checkEquals( diag(funx(x)), rep(list("foo"), 4) , 
+		msg = "matrix indexing lhs" )
+	
+	# drop dimensions
+	dim(x) <- NULL
+	checkException( funx(x) , msg = "not a matrix" )
+	
+	
+}
+

Modified: pkg/src/Rcpp/SEXP_Vector.h
===================================================================
--- pkg/src/Rcpp/SEXP_Vector.h	2010-01-26 09:16:34 UTC (rev 459)
+++ pkg/src/Rcpp/SEXP_Vector.h	2010-01-26 09:41:59 UTC (rev 460)
@@ -35,7 +35,7 @@
 	/* much inspired from item 30 of more effective C++ */
 	class Proxy {
 	public:
-		Proxy( SEXP_Vector<RTYPE>& v, size_t i ) : parent(v), index(i) {}
+		Proxy( SEXP_Vector& v, size_t i ) : parent(v), index(i) {}
 		
 		/* lvalue uses */
 		Proxy& operator=(const Proxy& rhs){
@@ -66,7 +66,7 @@
 		
 		
 	private:
-		SEXP_Vector<RTYPE>& parent; 
+		SEXP_Vector& parent; 
 		size_t index ;
 	} ;
 
@@ -88,13 +88,20 @@
 #endif
 	
 	const Proxy operator[]( int i ) const throw(index_out_of_bounds){
-		return Proxy(const_cast<SEXP_Vector<RTYPE>&>(*this), i) ;
+		return Proxy(const_cast<SEXP_Vector<RTYPE>&>(*this), offset(i)) ;
 	}
 	Proxy operator[]( int i ) throw(index_out_of_bounds){
-		if( i<0 || i>=length()) throw index_out_of_bounds() ;
-		return Proxy(*this, i ) ; 
+		return Proxy(*this, offset(i) ) ; 
 	}
 
+	
+	Proxy operator()( const size_t& i) throw(index_out_of_bounds){
+		return Proxy(*this, offset(i) ) ;
+	}
+	Proxy operator()( const size_t& i, const size_t& j) throw(index_out_of_bounds,not_a_matrix){
+		return Proxy(*this, offset(i,j) ) ;
+	}
+	
 	friend class Proxy; 
 	
 private:



More information about the Rcpp-commits mailing list