[Rcpp-commits] r3990 - in pkg/Rcpp: . inst/include/Rcpp/sugar/functions inst/include/Rcpp/sugar/functions/mapply inst/include/Rcpp/vector

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Nov 18 17:16:05 CET 2012


Author: romain
Date: 2012-11-18 17:16:04 +0100 (Sun, 18 Nov 2012)
New Revision: 3990

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply/mapply_2.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/pow.h
   pkg/Rcpp/inst/include/Rcpp/vector/converter.h
Log:
allow CharacterVector::create( NA_STRING )

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-11-17 15:25:06 UTC (rev 3989)
+++ pkg/Rcpp/ChangeLog	2012-11-18 16:16:04 UTC (rev 3990)
@@ -1,3 +1,8 @@
+2012-11-18  Romain Francois <romain at r-enthusiasts.com>
+
+        * include/Rcpp/vector/converter.h: allow CharacterVector::create( NA_STRING )
+        * include/Rcpp/sugar/functions/mapply/mapply_2.h: more cases in mapply
+
 2012-11-17  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/doc/Rcpp-modules/Rcpp-modules.Rnw: Change free function name

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply/mapply_2.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply/mapply_2.h	2012-11-17 15:25:06 UTC (rev 3989)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply/mapply_2.h	2012-11-18 16:16:04 UTC (rev 3990)
@@ -40,34 +40,107 @@
 public:         
 	typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
 	
-    typedef Rcpp::VectorBase<RTYPE,NA_1,T_1> VEC_1 ;
-	typedef Rcpp::VectorBase<RTYPE,NA_2,T_2> VEC_2 ;
+	Mapply_2( const T_1& vec_1_, const T_2& vec_2_, Function fun_ ) : 
+		vec_1(vec_1_), vec_2(vec_2_), fun(fun_){}
 	
-	typedef typename Rcpp::traits::Extractor<RTYPE,NA_1,T_1>::type EXT_1 ;
-	typedef typename Rcpp::traits::Extractor<RTYPE,NA_2,T_2>::type EXT_2 ;
+	inline result_type operator[]( int i ) const {
+		return fun( vec_1[i], vec_2[i] );
+	}
+	inline int size() const { return vec_1.size() ; }
+	         
+private:
+	const T_1& vec_1 ;
+	const T_2& vec_2 ;
+	Function fun ;
+} ;
+
+template <int RTYPE, 
+    bool NA_1, typename T_1, 
+    typename PRIM_2 , 
+    typename Function
+>
+class Mapply_2_Vector_Primitive : public 
+    VectorBase< 
+	    Rcpp::traits::r_sexptype_traits<
+	    	typename ::Rcpp::traits::result_of<Function>::type
+	    >::rtype , 
+	    true ,
+	    Mapply_2_Vector_Primitive<RTYPE,NA_1,T_1,PRIM_2,Function>
+    >
+{
+public:         
+	typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
 	
-	Mapply_2( const VEC_1& vec_1_, const VEC_2& vec_2_, Function fun_ ) : 
-		vec_1(vec_1_.get_ref()), vec_2(vec_2_.get_ref()), fun(fun_){}
+	Mapply_2_Vector_Primitive( const T_1& vec_1_, PRIM_2 prim_2_, Function fun_ ) : 
+		vec_1(vec_1_), prim_2(prim_2_), fun(fun_){}
 	
 	inline result_type operator[]( int i ) const {
-		return fun( vec_1[i], vec_2[i] );
+		return fun( vec_1[i], prim_2 );
 	}
 	inline int size() const { return vec_1.size() ; }
 	         
 private:
-	const EXT_1& vec_1 ;
-	const EXT_2& vec_2 ;
+	const T_1& vec_1 ;
+	PRIM_2 prim_2 ;
 	Function fun ;
 } ;
+
+template <int RTYPE, 
+    typename PRIM_1,
+    bool NA_2, typename T_2, 
+    typename Function
+>
+class Mapply_2_Primitive_Vector : public 
+    VectorBase< 
+	    Rcpp::traits::r_sexptype_traits<
+	    	typename ::Rcpp::traits::result_of<Function>::type
+	    >::rtype , 
+	    true ,
+	    Mapply_2_Primitive_Vector<RTYPE,PRIM_1,NA_2,T_2,Function>
+    >
+{
+public:         
+	typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
 	
+	Mapply_2_Primitive_Vector( PRIM_1 prim_1_, const T_2& vec_2_, Function fun_ ) : 
+		prim_1(prim_1_), vec_2(vec_2_), fun(fun_){}
+	
+	inline result_type operator[]( int i ) const {
+		return fun( prim_1, vec_2[i] );
+	}
+	inline int size() const { return vec_2.size() ; }
+	         
+private:
+	PRIM_1 prim_1 ;
+    const T_2& vec_2 ;
+	Function fun ;
+} ;
+
+
+
 } // sugar
+ 
 
 template <int RTYPE, bool NA_1, typename T_1, bool NA_2, typename T_2, typename Function >
 inline sugar::Mapply_2<RTYPE,NA_1,T_1,NA_2,T_2,Function> 
 mapply( const Rcpp::VectorBase<RTYPE,NA_1,T_1>& t1, const Rcpp::VectorBase<RTYPE,NA_2,T_2>& t2, Function fun ){
-	return sugar::Mapply_2<RTYPE,NA_1,T_1,NA_2,T_2,Function>( t1, t2, fun ) ;
+	return sugar::Mapply_2<RTYPE,NA_1,T_1,NA_2,T_2,Function>( t1.get_ref(), t2.get_ref(), fun ) ;
 }
 
+template <int RTYPE, bool NA_1, typename T_1, typename Function >
+inline sugar::Mapply_2_Vector_Primitive<RTYPE,NA_1,T_1,double,Function> 
+mapply( const Rcpp::VectorBase<RTYPE,NA_1,T_1>& t1, double t2, Function fun ){
+	return sugar::Mapply_2_Vector_Primitive<RTYPE,NA_1,T_1,double,Function>( t1.get_ref(), t2, fun ) ;
+}
+
+template <int RTYPE, bool NA_2, typename T_2, typename Function >
+inline sugar::Mapply_2_Primitive_Vector<RTYPE,double, NA_2,T_2,Function> 
+mapply( double t1, const Rcpp::VectorBase<RTYPE,NA_2,T_2>& t2, Function fun ){
+	return sugar::Mapply_2_Primitive_Vector<RTYPE,double, NA_2,T_2,Function>( t1, t2.get_ref(), fun ) ;
+}
+
+
+
 } // Rcpp
 
 #endif

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/pow.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/pow.h	2012-11-17 15:25:06 UTC (rev 3989)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/pow.h	2012-11-18 16:16:04 UTC (rev 3990)
@@ -25,69 +25,62 @@
 namespace Rcpp{
 namespace sugar{
 
-template <bool NA, int RTYPE, typename EXPONENT_TYPE>
-class pow__impl{
+template <int RTYPE, bool NA, typename T, typename EXPONENT_TYPE>
+class Pow : public Rcpp::VectorBase< REALSXP ,NA, Pow<RTYPE,NA,T,EXPONENT_TYPE> > {
 public:
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-	pow__impl( EXPONENT_TYPE exponent_) : exponent(exponent_){}
 	
-	inline double get( STORAGE x) const {
-		return Rcpp::traits::is_na<RTYPE>(x) ? NA_INTEGER : ::pow(x, exponent) ;
+	Pow( const T& object_, EXPONENT_TYPE exponent ) : object(object_), op(exponent) {}
+	
+	inline double operator[]( int i ) const {
+	    return ::pow( object[i], op );
 	}
+	inline int size() const { return object.size() ; }
+	         
 private:
-	EXPONENT_TYPE exponent ;
+	const T& object ;
+	EXPONENT_TYPE op ;
 } ;
-
-template <int RTYPE, typename EXPONENT_TYPE>
-class pow__impl<false,RTYPE,EXPONENT_TYPE>{
+	
+template <bool NA, typename T, typename EXPONENT_TYPE>
+class Pow<INTSXP,NA,T,EXPONENT_TYPE> : public Rcpp::VectorBase< REALSXP ,NA, Pow<INTSXP,NA,T,EXPONENT_TYPE> > {
 public:
-	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-	pow__impl( EXPONENT_TYPE exponent_) : exponent(exponent_){}
+	Pow( const T& object_, EXPONENT_TYPE exponent ) : object(object_), op(exponent) {}
 	
-	inline double get( STORAGE x) const {
-		return ::pow(x, exponent) ;
+	inline double operator[]( int i ) const {
+		int x = object[i] ;
+	    return x == NA_INTEGER ? NA_INTEGER : ::pow( x, op );
 	}
+	inline int size() const { return object.size() ; }
+	         
 private:
-	EXPONENT_TYPE exponent ;
+	const T& object ;
+	EXPONENT_TYPE op ;
 } ;
-
-	
-template <int RTYPE, bool NA, typename T, typename EXPONENT_TYPE>
-class Pow : public Rcpp::VectorBase< REALSXP ,NA, Pow<RTYPE,NA,T,EXPONENT_TYPE> > {
+template <typename T, typename EXPONENT_TYPE>
+class Pow<INTSXP,false,T,EXPONENT_TYPE> : public Rcpp::VectorBase< REALSXP ,false, Pow<INTSXP,false,T,EXPONENT_TYPE> > {
 public:
-	typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
-	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-	typedef pow__impl<NA,RTYPE, typename Rcpp::traits::remove_const<EXPONENT_TYPE>::type > OPERATOR ;
+	Pow( const T& object_, EXPONENT_TYPE exponent ) : object(object_), op(exponent) {}
 	
-	Pow( const VEC_TYPE& object_, EXPONENT_TYPE exponent ) : 
-		object(object_), op(exponent) {}
-	
 	inline double operator[]( int i ) const {
-		return op.get( object[i] );
+	    return ::pow( object[i], op );
 	}
 	inline int size() const { return object.size() ; }
 	         
 private:
-	const VEC_TYPE& object ;
-	OPERATOR op ;
+	const T& object ;
+	EXPONENT_TYPE op ;
 } ;
-	
-} // sugar
 
-template <bool NA, typename T, typename EXPONENT_TYPE >
-inline sugar::Pow<INTSXP,NA,T,EXPONENT_TYPE> pow( 
-	const VectorBase<INTSXP,NA,T>& t, 
-	EXPONENT_TYPE exponent 
-){
-	return sugar::Pow<INTSXP,NA,T,EXPONENT_TYPE>( t , exponent ) ;
-}
 
-template <bool NA, typename T, typename EXPONENT_TYPE>
-inline sugar::Pow<REALSXP,NA,T,EXPONENT_TYPE> pow( 
-	const VectorBase<REALSXP,NA,T>& t, 
+} // sugar
+
+template <int RTYPE, bool NA, typename T, typename EXPONENT_TYPE>
+inline sugar::Pow<RTYPE,NA,T,EXPONENT_TYPE> pow( 
+	const VectorBase<RTYPE,NA,T>& t, 
 	EXPONENT_TYPE exponent
 ){
-	return sugar::Pow<REALSXP,NA,T,EXPONENT_TYPE>( t, exponent ) ;
+	return sugar::Pow<RTYPE,NA,T,EXPONENT_TYPE>( t.get_ref() , exponent ) ;
 }
 
 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/converter.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/converter.h	2012-11-17 15:25:06 UTC (rev 3989)
+++ pkg/Rcpp/inst/include/Rcpp/vector/converter.h	2012-11-18 16:16:04 UTC (rev 3990)
@@ -56,6 +56,9 @@
 		static SEXP get(const char& input){
 			return Rf_mkChar( &input ) ;
 		}
+		
+		// assuming a CHARSXP
+		static SEXP get(SEXP x){ return x; }
 	} ;
 	
 	template <int RTYPE>



More information about the Rcpp-commits mailing list