[Rcpp-commits] r925 - in pkg/Rcpp: inst inst/unitTests src/Rcpp
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Mar 20 09:37:00 CET 2010
Author: romain
Date: 2010-03-20 09:36:59 +0100 (Sat, 20 Mar 2010)
New Revision: 925
Modified:
pkg/Rcpp/inst/ChangeLog
pkg/Rcpp/inst/unitTests/runit.IntegerVector.R
pkg/Rcpp/src/Rcpp/Vector.h
Log:
+ Vector<>::fill<>
Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog 2010-03-19 18:28:48 UTC (rev 924)
+++ pkg/Rcpp/inst/ChangeLog 2010-03-20 08:36:59 UTC (rev 925)
@@ -1,3 +1,8 @@
+2010-03-20 Romain Francois <romain at r-enthusiasts.com>
+
+ * src/Rcpp/Vector.h : Vector gains a templated fill method to fill all
+ elements of a vector with the same value
+
2010-03-19 Romain Francois <romain at r-enthusiasts.com>
* src/Rcpp/Vector.h : Matrix gains a diag method to create diagonal
Modified: pkg/Rcpp/inst/unitTests/runit.IntegerVector.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.IntegerVector.R 2010-03-19 18:28:48 UTC (rev 924)
+++ pkg/Rcpp/inst/unitTests/runit.IntegerVector.R 2010-03-20 08:36:59 UTC (rev 925)
@@ -251,3 +251,14 @@
}
+test.IntegerVector.fill <- function(){
+ funx <- cfunction(signature(x = "integer"), '
+ IntegerVector y(x) ;
+ y.fill(10) ;
+ return y ;',
+ Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+ x <- 1:10
+ funx(x)
+ checkEquals( x, rep(10L, 10 ), msg = "IntegerVector.fill" )
+}
+
Modified: pkg/Rcpp/src/Rcpp/Vector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/Vector.h 2010-03-19 18:28:48 UTC (rev 924)
+++ pkg/Rcpp/src/Rcpp/Vector.h 2010-03-20 08:36:59 UTC (rev 925)
@@ -413,6 +413,10 @@
typedef bool type ;
} ;
+ template <int RTYPE> struct is_trivial : public true_type{} ;
+ template <> struct is_trivial<VECSXP> : public false_type{} ;
+ template <> struct is_trivial<EXPRSXP> : public false_type{} ;
+
} // traits
template <typename VECTOR>
@@ -465,6 +469,12 @@
init() ;
}
+ template <typename U>
+ Vector( const size_t& size, const U& u){
+ Base::setSEXP( Rf_allocVector( RTYPE, size) ) ;
+ fill( u ) ;
+ }
+
Vector( const Dimension& dims) : Base() {
Base::setSEXP( Rf_allocVector( RTYPE, dims.prod() ) ) ;
init() ;
@@ -473,6 +483,16 @@
}
}
+ template <typename U>
+ Vector( const Dimension& dims, const U& u) : Base() {
+ Base::setSEXP( Rf_allocVector( RTYPE, dims.prod() ) ) ;
+ fill(u) ;
+ if( dims.size() > 1 ){
+ Base::attr( "dim" ) = dims;
+ }
+ }
+
+
internal::ListInitialization<iterator,init_type> operator=( init_type x){
iterator start = begin() ; *start = x;
return internal::ListInitialization<iterator,init_type>( start + 1 ) ; ;
@@ -553,6 +573,32 @@
return -1 ; /* -Wall */
}
+ private:
+
+ template <typename U>
+ void fill_dispatch( traits::false_type, const U& u){
+ // when this is not trivial, this is SEXP
+ SEXP elem = PROTECT( converter_type::get( u ) );
+ iterator it(begin());
+ for( int i=0; i<size ; i++, ++it){
+ *it = ::Rf_duplicate( elem ) ;
+ }
+ UNPROTECT(1) ; /* elem */
+ }
+
+ template <typename U>
+ void fill__dispatch( traits::true_type, const U& u){
+ std::fill( begin(), end(), converter_type::get( u ) ) ;
+ }
+
+ public:
+
+ template <typename U>
+ void fill( const U& u){
+ fill__dispatch( typename traits::is_trivial<RTYPE>::type(), u ) ;
+ }
+
+
/* TODO: 3 dimensions, ... n dimensions through variadic templates */
class NamesProxy {
@@ -682,7 +728,7 @@
void update_vector(){
cache.update(*this) ;
}
-
+
/* <code-bloat>
public:
@@ -2199,7 +2245,7 @@
VECTOR::setSEXP( x ) ;
}
- Matrix& operator=(const Matrix& other) {
+ Matrix& operator=(const Matrix& other) {
SEXP x = other.asSexp() ;
if( ! ::Rf_isMatrix(x) ) throw RObject::not_compatible("not a matrix") ;
VECTOR::setSEXP( x ) ;
More information about the Rcpp-commits
mailing list