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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jan 26 10:16:35 CET 2010


Author: romain
Date: 2010-01-26 10:16:34 +0100 (Tue, 26 Jan 2010)
New Revision: 459

Modified:
   pkg/inst/ChangeLog
   pkg/inst/unitTests/runit.CharacterVector.R
   pkg/inst/unitTests/runit.IntegerVector.R
   pkg/inst/unitTests/runit.NumericVector.R
   pkg/src/CharacterVector.cpp
   pkg/src/Rcpp/CharacterVector.h
   pkg/src/Rcpp/VectorBase.h
   pkg/src/VectorBase.cpp
Log:
matrix indexing for character vectors

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-26 08:53:59 UTC (rev 458)
+++ pkg/inst/ChangeLog	2010-01-26 09:16:34 UTC (rev 459)
@@ -12,7 +12,13 @@
 
 	* src/Rcpp/SimpleVector.h: outsourcing offset calculation 
 	to VectorBase::offset
-	
+
+	* src/Rcpp/CharacterVector.h: use offset to implement matrix-like
+	indexing for matrices of strings
+
+	* inst/unitTests/runit.CharacterVector.R: unit test for 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.CharacterVector.R
===================================================================
--- pkg/inst/unitTests/runit.CharacterVector.R	2010-01-26 08:53:59 UTC (rev 458)
+++ pkg/inst/unitTests/runit.CharacterVector.R	2010-01-26 09:16:34 UTC (rev 459)
@@ -68,3 +68,30 @@
 		msg = "StringProxy::operator+=" )
 }
 
+test.CharacterVector.matrix.indexing <- function(){
+
+	funx <- cfunction(signature(x = "character" ), '
+		CharacterVector m(x) ;
+		std::string trace  ;
+		for( size_t i=0 ; i<4; i++){
+			trace += m(i,i) ;
+		}
+		return wrap( trace ) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	x <- matrix( as.character(1:16), ncol = 4 )
+	checkEquals( funx(x), paste(diag(x), collapse = ""), msg = "matrix indexing" )
+	
+	y <- as.vector( x )
+	checkException( funx(y) , msg = "not a matrix" )
+	
+	funx <- cfunction(signature(x = "integer" ), '
+		CharacterVector 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("foo", 4) , 
+		msg = "matrix indexing lhs" )
+}
+

Modified: pkg/inst/unitTests/runit.IntegerVector.R
===================================================================
--- pkg/inst/unitTests/runit.IntegerVector.R	2010-01-26 08:53:59 UTC (rev 458)
+++ pkg/inst/unitTests/runit.IntegerVector.R	2010-01-26 09:16:34 UTC (rev 459)
@@ -71,7 +71,6 @@
 	
 	funx <- cfunction(signature(x = "integer" ), '
 		IntegerVector m(x) ;
-		int trace = 0.0 ;
 		for( size_t i=0 ; i<4; i++){
 			m(i,i) = 2 * i ;
 		}

Modified: pkg/inst/unitTests/runit.NumericVector.R
===================================================================
--- pkg/inst/unitTests/runit.NumericVector.R	2010-01-26 08:53:59 UTC (rev 458)
+++ pkg/inst/unitTests/runit.NumericVector.R	2010-01-26 09:16:34 UTC (rev 459)
@@ -70,7 +70,6 @@
 	
 	funx <- cfunction(signature(x = "numeric" ), '
 		NumericVector m(x) ;
-		double trace = 0.0 ;
 		for( size_t i=0 ; i<4; i++){
 			m(i,i) = 2.0 * i ;
 		}

Modified: pkg/src/CharacterVector.cpp
===================================================================
--- pkg/src/CharacterVector.cpp	2010-01-26 08:53:59 UTC (rev 458)
+++ pkg/src/CharacterVector.cpp	2010-01-26 09:16:34 UTC (rev 459)
@@ -80,15 +80,20 @@
 
 
 const CharacterVector::StringProxy CharacterVector::operator[](int i) const throw(index_out_of_bounds){
-	if( i<0 || i>=length()) throw index_out_of_bounds() ;
-	return StringProxy(const_cast<CharacterVector&>(*this), i) ;
+	return StringProxy(const_cast<CharacterVector&>(*this), offset(i) ) ;
 }
 
 CharacterVector::StringProxy CharacterVector::operator[](int i) throw(index_out_of_bounds) {
-	if( i<0 || i>=length()) throw index_out_of_bounds() ;
-	return StringProxy(*this, i ) ;
+	return StringProxy(*this, offset(i) ) ;
 }
 
+CharacterVector::StringProxy CharacterVector::operator()( const size_t& i) throw(index_out_of_bounds){
+	return StringProxy(*this, offset(i) ) ;
+}
 
+CharacterVector::StringProxy CharacterVector::operator()( const size_t& i, const size_t&j ) throw(index_out_of_bounds,not_a_matrix){
+	return StringProxy(*this, offset(i,j) ) ;
+}
 
+
 } // namespace 

Modified: pkg/src/Rcpp/CharacterVector.h
===================================================================
--- pkg/src/Rcpp/CharacterVector.h	2010-01-26 08:53:59 UTC (rev 458)
+++ pkg/src/Rcpp/CharacterVector.h	2010-01-26 09:16:34 UTC (rev 459)
@@ -67,7 +67,11 @@
 	const StringProxy operator[]( int i ) const throw(index_out_of_bounds);
 	StringProxy operator[]( int i ) throw(index_out_of_bounds);
 
-	friend class StringProxy; 
+	friend class StringProxy;
+	
+	/* '(' indexing */
+	StringProxy operator()( const size_t& i) throw(index_out_of_bounds) ;
+	StringProxy operator()( const size_t& i, const size_t& j) throw(index_out_of_bounds,not_a_matrix) ;
 
 private:
 	template <typename InputIterator>

Modified: pkg/src/Rcpp/VectorBase.h
===================================================================
--- pkg/src/Rcpp/VectorBase.h	2010-01-26 08:53:59 UTC (rev 458)
+++ pkg/src/Rcpp/VectorBase.h	2010-01-26 09:16:34 UTC (rev 459)
@@ -55,7 +55,11 @@
      */
     size_t offset(const size_t& i, const size_t& j) const throw(not_a_matrix,RObject::index_out_of_bounds) ;
     
-    size_t offset(const size_t& i) throw(RObject::index_out_of_bounds) ;
+    /**
+     * one dimensional offset doing bounds checking to ensure
+     * it is valid
+     */
+    size_t offset(const size_t& i) const throw(RObject::index_out_of_bounds);
     
     /* TODO: 3 dimensions, ... n dimensions through variadic templates */
     

Modified: pkg/src/VectorBase.cpp
===================================================================
--- pkg/src/VectorBase.cpp	2010-01-26 08:53:59 UTC (rev 458)
+++ pkg/src/VectorBase.cpp	2010-01-26 09:16:34 UTC (rev 459)
@@ -38,7 +38,7 @@
 		return i + nrow*j ;
 	}
 
-	size_t VectorBase::offset(const size_t& i) throw(RObject::index_out_of_bounds){
+	size_t VectorBase::offset(const size_t& i) const throw(RObject::index_out_of_bounds){
     	    if( i >= static_cast<size_t>(Rf_length(m_sexp)) ) throw RObject::index_out_of_bounds() ;
     	    return i ;
     	}



More information about the Rcpp-commits mailing list