[Rcpp-commits] r2817 - in pkg/Rcpp: . inst/include/Rcpp/sugar/operators inst/unitTests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Dec 23 12:13:20 CET 2010
Author: romain
Date: 2010-12-23 12:13:16 +0100 (Thu, 23 Dec 2010)
New Revision: 2817
Modified:
pkg/Rcpp/ChangeLog
pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h
pkg/Rcpp/inst/unitTests/runit.support.R
Log:
more efficient operator*
Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog 2010-12-23 10:48:56 UTC (rev 2816)
+++ pkg/Rcpp/ChangeLog 2010-12-23 11:13:16 UTC (rev 2817)
@@ -3,6 +3,9 @@
* inst/include/Rcpp/RObject.h: New internal class SEXPstack to handle
garbage collection. Presumably more efficient than R_PreserveObject and
R_ReleaseObject
+
+ * inst/include/Rcpp/sugar/operators/times.h: More efficient operator* for
+ REALSXP sugar expressions, avoiding unnecessary NA tests.
2010-12-22 Romain Francois <romain at r-enthusiasts.com>
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h 2010-12-23 10:48:56 UTC (rev 2816)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h 2010-12-23 11:13:16 UTC (rev 2817)
@@ -51,7 +51,33 @@
private:
const LHS_EXT& lhs ;
const RHS_EXT& rhs ;
- } ;
+ } ;
+ // RTYPE = REALSXP
+ template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T >
+ class Times_Vector_Vector<REALSXP,LHS_NA,LHS_T,RHS_NA,RHS_T> :
+ public Rcpp::VectorBase<REALSXP, true , Times_Vector_Vector<REALSXP,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
+ public:
+ typedef typename Rcpp::VectorBase<REALSXP,LHS_NA,LHS_T> LHS_TYPE ;
+ typedef typename Rcpp::VectorBase<REALSXP,RHS_NA,RHS_T> RHS_TYPE ;
+
+ typedef typename Rcpp::traits::Extractor<REALSXP, LHS_NA, LHS_T>::type LHS_EXT ;
+ typedef typename Rcpp::traits::Extractor<REALSXP, RHS_NA, RHS_T>::type RHS_EXT ;
+
+ Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
+ lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {
+ }
+
+ inline double operator[]( int i ) const {
+ return lhs[i] * rhs[i];
+ }
+
+ inline int size() const { return lhs.size() ; }
+
+ private:
+ const LHS_EXT& lhs ;
+ const RHS_EXT& rhs ;
+ } ;
+
// specialization LHS_NA = false
template <int RTYPE, typename LHS_T, bool RHS_NA, typename RHS_T >
@@ -78,7 +104,32 @@
private:
const LHS_EXT& lhs ;
const RHS_EXT& rhs ;
- } ;
+ } ;
+ // RTYPE = REALSXP
+ template <typename LHS_T, bool RHS_NA, typename RHS_T >
+ class Times_Vector_Vector<REALSXP,false,LHS_T,RHS_NA,RHS_T> :
+ public Rcpp::VectorBase<REALSXP,true, Times_Vector_Vector<REALSXP,false,LHS_T,RHS_NA,RHS_T> > {
+ public:
+ typedef typename Rcpp::VectorBase<REALSXP,false,LHS_T> LHS_TYPE ;
+ typedef typename Rcpp::VectorBase<REALSXP,RHS_NA,RHS_T> RHS_TYPE ;
+
+ typedef typename Rcpp::traits::Extractor<REALSXP, false, LHS_T>::type LHS_EXT ;
+ typedef typename Rcpp::traits::Extractor<REALSXP, RHS_NA, RHS_T>::type RHS_EXT ;
+
+ Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
+ lhs(lhs_.get_ref()), rhs(rhs_.get_ref()){}
+
+ inline double operator[]( int i ) const {
+ return lhs[i] * rhs[i] ;
+ }
+
+ inline int size() const { return lhs.size() ; }
+
+ private:
+ const LHS_EXT& lhs ;
+ const RHS_EXT& rhs ;
+ } ;
+
// specialization for RHS_NA = false
template <int RTYPE, bool LHS_NA, typename LHS_T, typename RHS_T >
@@ -105,9 +156,32 @@
private:
const LHS_EXT& lhs ;
const RHS_EXT& rhs ;
+ } ;
+ // RTYPE = REALSXP
+ template <bool LHS_NA, typename LHS_T, typename RHS_T >
+ class Times_Vector_Vector<REALSXP,LHS_NA,LHS_T,false,RHS_T> :
+ public Rcpp::VectorBase<REALSXP, true , Times_Vector_Vector<REALSXP,LHS_NA,LHS_T,false,RHS_T> > {
+ public:
+ typedef typename Rcpp::VectorBase<REALSXP,LHS_NA,LHS_T> LHS_TYPE ;
+ typedef typename Rcpp::VectorBase<REALSXP,false,RHS_T> RHS_TYPE ;
+
+ typedef typename Rcpp::traits::Extractor<REALSXP, LHS_NA, LHS_T>::type LHS_EXT ;
+ typedef typename Rcpp::traits::Extractor<REALSXP, false, RHS_T>::type RHS_EXT ;
+
+ Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
+ lhs(lhs_.get_ref()), rhs(rhs_.get_ref()){}
+
+ inline double operator[]( int i ) const {
+ return lhs[i] * rhs[i] ;
+ }
+
+ inline int size() const { return lhs.size() ; }
+
+ private:
+ const LHS_EXT& lhs ;
+ const RHS_EXT& rhs ;
} ;
-
// specialization for RHS_NA = false and LHS_NA = false
template <int RTYPE, typename LHS_T, typename RHS_T >
class Times_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> : public Rcpp::VectorBase<RTYPE, false , Times_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> > {
@@ -132,9 +206,32 @@
const LHS_EXT& lhs ;
const RHS_EXT& rhs ;
} ;
+ // RTYPE = REALSXP
+ template <typename LHS_T, typename RHS_T >
+ class Times_Vector_Vector<REALSXP,false,LHS_T,false,RHS_T> :
+ public Rcpp::VectorBase<REALSXP, false , Times_Vector_Vector<REALSXP,false,LHS_T,false,RHS_T> > {
+ public:
+ typedef typename Rcpp::VectorBase<REALSXP,false,LHS_T> LHS_TYPE ;
+ typedef typename Rcpp::VectorBase<REALSXP,false,RHS_T> RHS_TYPE ;
+
+ typedef typename Rcpp::traits::Extractor<REALSXP, false, LHS_T>::type LHS_EXT ;
+ typedef typename Rcpp::traits::Extractor<REALSXP, false, RHS_T>::type RHS_EXT ;
+
+ Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
+ lhs(lhs_.get_ref()), rhs(rhs_.get_ref()){}
+
+ inline double operator[]( int i ) const {
+ return lhs[i] * rhs[i] ;
+ }
+
+ inline int size() const { return lhs.size() ; }
+
+ private:
+ const LHS_EXT& lhs ;
+ const RHS_EXT& rhs ;
+ } ;
+
-
-
template <int RTYPE, bool NA, typename T>
class Times_Vector_Primitive : public Rcpp::VectorBase<RTYPE,true, Times_Vector_Primitive<RTYPE,NA,T> > {
public:
@@ -159,7 +256,28 @@
const EXT& lhs ;
STORAGE rhs ;
bool rhs_na ;
+ } ;
+ // RTYPE = REALSXP
+ template <bool NA, typename T>
+ class Times_Vector_Primitive<REALSXP,NA,T> :
+ public Rcpp::VectorBase<REALSXP,true, Times_Vector_Primitive<REALSXP,NA,T> > {
+ public:
+ typedef typename Rcpp::VectorBase<REALSXP,NA,T> VEC_TYPE ;
+ typedef typename Rcpp::traits::Extractor<REALSXP, NA, T>::type EXT ;
+ Times_Vector_Primitive( const VEC_TYPE& lhs_, double rhs_ ) :
+ lhs(lhs_.get_ref()), rhs(rhs_)
+ {}
+
+ inline double operator[]( int i ) const {
+ return rhs * lhs[i] ;
+ }
+
+ inline int size() const { return lhs.size() ; }
+
+ private:
+ const EXT& lhs ;
+ double rhs ;
} ;
@@ -186,6 +304,27 @@
bool rhs_na ;
} ;
+ // RTYPE = REALSXP
+ template <typename T>
+ class Times_Vector_Primitive<REALSXP,false,T> :
+ public Rcpp::VectorBase<REALSXP,false, Times_Vector_Primitive<REALSXP,false,T> > {
+ public:
+ typedef typename Rcpp::VectorBase<REALSXP,false,T> VEC_TYPE ;
+ typedef typename Rcpp::traits::Extractor<REALSXP, false, T>::type EXT ;
+
+ Times_Vector_Primitive( const VEC_TYPE& lhs_, double rhs_ ) :
+ lhs(lhs_.get_ref()), rhs(rhs_) {}
+
+ inline double operator[]( int i ) const {
+ return rhs * lhs[i] ;
+ }
+
+ inline int size() const { return lhs.size() ; }
+
+ private:
+ const EXT& lhs ;
+ double rhs ;
+ } ;
@@ -193,7 +332,6 @@
// Vector * nona(primitive)
-
template <int RTYPE, bool NA, typename T>
class Times_Vector_Primitive_nona : public Rcpp::VectorBase<RTYPE,true, Times_Vector_Primitive_nona<RTYPE,NA,T> > {
public:
@@ -217,6 +355,28 @@
STORAGE rhs ;
} ;
+ // RTYPE = REALSXP
+ template <bool NA, typename T>
+ class Times_Vector_Primitive_nona<REALSXP,NA,T> :
+ public Rcpp::VectorBase<REALSXP,true, Times_Vector_Primitive_nona<REALSXP,NA,T> > {
+ public:
+ typedef typename Rcpp::VectorBase<REALSXP,NA,T> VEC_TYPE ;
+ typedef typename Rcpp::traits::Extractor<REALSXP,NA,T>::type EXT ;
+
+ Times_Vector_Primitive_nona( const VEC_TYPE& lhs_, double rhs_ ) :
+ lhs(lhs_.get_ref()), rhs(rhs_)
+ {}
+
+ inline double operator[]( int i ) const {
+ return lhs[i] * rhs ;
+ }
+
+ inline int size() const { return lhs.size() ; }
+
+ private:
+ const EXT& lhs ;
+ double rhs ;
+ } ;
template <int RTYPE, typename T>
@@ -241,6 +401,26 @@
STORAGE rhs ;
} ;
+ template <typename T>
+ class Times_Vector_Primitive_nona<REALSXP,false,T> :
+ public Rcpp::VectorBase<REALSXP,false, Times_Vector_Primitive_nona<REALSXP,false,T> > {
+ public:
+ typedef typename Rcpp::VectorBase<REALSXP,false,T> VEC_TYPE ;
+ typedef typename Rcpp::traits::Extractor<REALSXP, false, T>::type EXT ;
+
+ Times_Vector_Primitive_nona( const VEC_TYPE& lhs_, double rhs_ ) :
+ lhs(lhs_.get_ref()), rhs(rhs_) {}
+
+ inline double operator[]( int i ) const {
+ return rhs * lhs[i] ;
+ }
+
+ inline int size() const { return lhs.size() ; }
+
+ private:
+ const EXT& lhs ;
+ double rhs ;
+ } ;
}
}
Modified: pkg/Rcpp/inst/unitTests/runit.support.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.support.R 2010-12-23 10:48:56 UTC (rev 2816)
+++ pkg/Rcpp/inst/unitTests/runit.support.R 2010-12-23 11:13:16 UTC (rev 2817)
@@ -26,7 +26,18 @@
NA_REAL + 1.0,
1.0 + NA_REAL
);
- ')
+ '),
+ "times_REALSXP" = list(
+ signature(),
+ '
+ return List::create(
+ NA_REAL * NA_REAL,
+ NA_REAL * 1.0,
+ 1.0 * NA_REAL
+ );
+
+ '
+ )
)
}
.setUp <- function() {
@@ -45,3 +56,11 @@
msg = " REALSXP + REALSXP" )
}
+test.times.REALSXP <- function(){
+ fun <- .rcpp.support$times_REALSXP
+ checkEquals(
+ fun(),
+ list(NA_real_,NA_real_,NA_real_) ,
+ msg = " REALSXP * REALSXP" )
+}
+
More information about the Rcpp-commits
mailing list