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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jan 28 14:37:37 CET 2010


Author: romain
Date: 2010-01-28 14:37:37 +0100 (Thu, 28 Jan 2010)
New Revision: 504

Modified:
   pkg/inst/ChangeLog
   pkg/inst/unitTests/runit.GenericVector.R
   pkg/inst/unitTests/runit.IntegerVector.R
   pkg/src/Rcpp/SimpleVector.h
Log:
SimpleVector gains assign template and range based constructor

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-28 13:02:12 UTC (rev 503)
+++ pkg/inst/ChangeLog	2010-01-28 13:37:37 UTC (rev 504)
@@ -1,5 +1,10 @@
 2010-01-28  Romain Francois <francoisromain at free.fr>
 
+	* src/Rcpp/SimpleVector.h: simple vectors gain a range
+	based assign method and a range based assign constructor
+	* inst/unitTests/runit.IntegerVector.R: new unit test 
+	
+	
 	* src/Rcpp/Dimension.h: new class Rcpp::Dimension to support
 	creation of vectors with dimensions
 	
@@ -8,9 +13,10 @@
 	* src/Rcpp/SimpleVector.h: data is initialized with 0 when 
 	the object is constructed from size or Dimension constructor
 	
-	* inst/runit.CharacterVector.R: new unit test to test 
+	* inst/unitTestsrunit.CharacterVector.R: new unit test to test 
 	constructors using Dimension
-	* inst/runit.IntegerVector.R: idem
+	* inst/unitTests/runit.IntegerVector.R: idem
+	* inst/unitTests/runit.GenericVector.R: idem
 	
 	* R/unit.tests.R: new unexported function "test" to trigger
 	unit tests using installed test cases

Modified: pkg/inst/unitTests/runit.GenericVector.R
===================================================================
--- pkg/inst/unitTests/runit.GenericVector.R	2010-01-28 13:02:12 UTC (rev 503)
+++ pkg/inst/unitTests/runit.GenericVector.R	2010-01-28 13:37:37 UTC (rev 504)
@@ -94,7 +94,30 @@
 	# drop dimensions
 	dim(x) <- NULL
 	checkException( funx(x) , msg = "not a matrix" )
+}
+
+test.List.Dimension.constructor <- function(){
+
+	funx <- cfunction(signature(), '
+		return List( Dimension( 5 ) ) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	checkEquals( funx(), 
+		rep(list(NULL),5) , 
+		msg = "List( Dimension(5))" )
 	
+	funx <- cfunction(signature(), '
+		return List( Dimension( 5, 5 ) ) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	checkEquals( funx(), 
+		structure( rep( list(NULL), 25), dim = c(5,5) ),
+		msg = "List( Dimension(5,5))" )
 	
+	funx <- cfunction(signature(), '
+		return List( Dimension( 2, 3, 4) ) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	checkEquals( funx(), 
+		array( rep(list(NULL)), dim = c(2,3,4) ) , 
+		msg = "List( Dimension(2,3,4))" )
 }
 
+

Modified: pkg/inst/unitTests/runit.IntegerVector.R
===================================================================
--- pkg/inst/unitTests/runit.IntegerVector.R	2010-01-28 13:02:12 UTC (rev 503)
+++ pkg/inst/unitTests/runit.IntegerVector.R	2010-01-28 13:37:37 UTC (rev 504)
@@ -104,4 +104,21 @@
 		msg = "IntegerVector( Dimension(2,3,4))" )
 }
 
+test.IntegerVector.range.constructors <- function(){
 
+	funx <- cfunction(signature(), '
+		int x[] = { 0, 1, 2, 3 } ;
+		IntegerVector y( x, x+4 ) ;
+		return y;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	checkEquals( funx(), 0:3, msg = "assign(int*, int*)" )
+	
+	funx <- cfunction(signature(), '
+		std::vector<int> vec(4) ;
+		for( size_t i = 0; i<4; i++) vec[i] = i;
+		IntegerVector y( vec.begin(), vec.end() ) ;
+		return y;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	checkEquals( funx(), 0:4, msg = "assign(int*, int*)" )
+}
+

Modified: pkg/src/Rcpp/SimpleVector.h
===================================================================
--- pkg/src/Rcpp/SimpleVector.h	2010-01-28 13:02:12 UTC (rev 503)
+++ pkg/src/Rcpp/SimpleVector.h	2010-01-28 13:37:37 UTC (rev 504)
@@ -53,9 +53,14 @@
 		}
 	}
 	
+	template <typename InputIterator>
+	SimpleVector( InputIterator first, InputIterator last) : VectorBase(), start(){
+		assign( first, last ) ;
+	}
+	
 #ifdef HAS_INIT_LISTS
 	SimpleVector( std::initializer_list<CTYPE> list ) : VectorBase(), start(0){
-		simple_fill( list.begin() , list.end() ) ;
+		assign( list.begin() , list.end() ) ;
 	}
 #endif
 
@@ -70,35 +75,30 @@
 	inline CTYPE& operator()( const size_t& i, const size_t& j) throw(VectorBase::not_a_matrix,RObject::index_out_of_bounds){
 		return start[ offset(i,j) ] ;
 	}
-
-protected:
-	/* TODO: make this private ASAP */
-	CTYPE* start ;
 	
-private:
-	virtual void update(){ start = get_pointer<RTYPE,CTYPE>(m_sexp) ; }
-	
-	// simple is when there is no need for coercion
-	// called only when the input container contains doubles
-	// TODO: use traits or something to make sure this is an 
-	//       iterator over RTYPE
+	// TODO : deal with coercion by dispatching the call using 
+	//        the iterator traits, but for this we need a smart 
+	//        coerce template
 	template <typename InputIterator>
-	void simple_fill( InputIterator first, InputIterator last){
-		size_t size = std::distance( first, last) ;
-		SEXP x = PROTECT( Rf_allocVector( RTYPE, size ) );
+	void assign( InputIterator first, InputIterator last){
+		size_t size = std::distance( first, last ) ;
+		SEXP x = PROTECT(Rf_allocVector( RTYPE, size )) ;
 		std::copy( first, last, get_pointer<RTYPE,CTYPE>(x) ) ;
-		setSEXP( x ) ;
+		setSEXP(x) ;
 		UNPROTECT(1) ;
 	}
+
+private:
+	CTYPE* start ;
 	
+	virtual void update(){ start = get_pointer<RTYPE,CTYPE>(m_sexp) ; }
+	
 	void init(){
-		size_t n = static_cast<size_t>(length()) ;
-		CTYPE zero = static_cast<CTYPE>(0) ;
-		for( size_t i=0; i<n; i++){
-			start[i] = zero;
-		}
+		init( static_cast<CTYPE>(0) ) ;
 	}
-	
+	void init( const CTYPE& value){
+		std::fill( start, start+length(), value ) ;
+	}
 } ;
 
 }// namespace Rcpp



More information about the Rcpp-commits mailing list