[Rcpp-commits] r1609 - in pkg/Rcpp/inst: include include/Rcpp/sugar/functions include/Rcpp/traits unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 18 19:03:04 CEST 2010


Author: romain
Date: 2010-06-18 19:03:04 +0200 (Fri, 18 Jun 2010)
New Revision: 1609

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/sapply.h
   pkg/Rcpp/inst/include/Rcpp/traits/result_of.h
   pkg/Rcpp/inst/unitTests/runit.sugar.sapply.R
Modified:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
   pkg/Rcpp/inst/include/RcppCommon.h
Log:
enters sapply

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-06-18 14:08:45 UTC (rev 1608)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-06-18 17:03:04 UTC (rev 1609)
@@ -26,5 +26,6 @@
 #include <Rcpp/sugar/functions/all.h>
 #include <Rcpp/sugar/functions/is_na.h>
 #include <Rcpp/sugar/functions/seq_along.h>
+#include <Rcpp/sugar/functions/sapply.h>
 
 #endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/sapply.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/sapply.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/sapply.h	2010-06-18 17:03:04 UTC (rev 1609)
@@ -0,0 +1,60 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// sapply.h: Rcpp R/C++ interface class library -- sapply
+//
+// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef Rcpp__sugar__sapply_h
+#define Rcpp__sugar__sapply_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool NA, typename T, typename Function>
+class Sapply : public VectorBase< 
+	Rcpp::traits::r_sexptype_traits<typename ::Rcpp::traits::result_of<Function>::type >::rtype , 
+	true ,
+	Sapply<RTYPE,NA,T,Function>
+> {
+public:         
+	typedef Rcpp::VectorBase<RTYPE,NA,T> VEC ;
+	typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
+	
+	Sapply( const VEC& vec_, Function fun_ ) : vec(vec_), fun(fun_){}
+	
+	inline result_type operator[]( int i ) const {
+		return fun( vec[i] ) ;
+	}
+	inline int size() const { return vec.size() ; }
+	         
+private:
+	const VEC& vec ;
+	Function fun ;
+} ;
+	
+} // sugar
+
+template <int RTYPE, bool _NA_, typename T, typename Function >
+inline sugar::Sapply<RTYPE,_NA_,T,Function> 
+sapply( const Rcpp::VectorBase<RTYPE,_NA_,T>& t, Function fun ){
+	return sugar::Sapply<RTYPE,_NA_,T,Function>( t, fun ) ;
+}
+
+} // Rcpp
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/traits/result_of.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/result_of.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/traits/result_of.h	2010-06-18 17:03:04 UTC (rev 1609)
@@ -0,0 +1,43 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */
+//
+// result_of.h: Rcpp R/C++ interface class library -- traits to help wrap
+//
+// Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef Rcpp__traits__result_of__h
+#define Rcpp__traits__result_of__h
+
+namespace Rcpp{
+namespace traits{
+
+template <typename T>
+struct result_of{
+	typedef typename T::result_type type ;
+} ;
+
+template <typename RESULT_TYPE, typename INPUT_TYPE>
+struct result_of< RESULT_TYPE (*)(INPUT_TYPE) >{
+	typedef RESULT_TYPE type ;
+} ;
+
+}
+}     
+
+#endif
+

Modified: pkg/Rcpp/inst/include/RcppCommon.h
===================================================================
--- pkg/Rcpp/inst/include/RcppCommon.h	2010-06-18 14:08:45 UTC (rev 1608)
+++ pkg/Rcpp/inst/include/RcppCommon.h	2010-06-18 17:03:04 UTC (rev 1609)
@@ -233,6 +233,7 @@
 #include <Rcpp/traits/remove_const.h>
 #include <Rcpp/traits/remove_reference.h>
 #include <Rcpp/traits/remove_const_and_reference.h>
+#include <Rcpp/traits/result_of.h>
 
 #include <Rcpp/internal/caster.h>
 #include <Rcpp/internal/r_vector.h>

Added: pkg/Rcpp/inst/unitTests/runit.sugar.sapply.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.sapply.R	                        (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.sapply.R	2010-06-18 17:03:04 UTC (rev 1609)
@@ -0,0 +1,59 @@
+#!/usr/bin/r -t
+#
+# Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+#
+# This file is part of Rcpp.
+#
+# Rcpp is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# Rcpp is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+test.sugar.sapply <- function( ){
+
+	inc <- '
+	template <typename T>
+	class square : public std::unary_function<T,T> {
+	public:
+		T operator()( T t) const { return t*t ; }
+	} ;
+	'
+	
+	fx <- cxxfunction( signature( x = "numeric" ), '
+	
+		NumericVector xx(x) ;
+		NumericVector res = sapply( xx, square<double>() );
+		
+		return res ;
+	
+	', include = inc, plugin = "Rcpp" )
+	
+	checkEquals( fx(1:10) , (1:10)^2 )
+}
+
+test.sugar.sapply.rawfun <- function( ){
+
+	inc <- '
+	double square( double x){ return x*x; }
+	'
+	
+	fx <- cxxfunction( signature( x = "numeric" ), '
+	
+		NumericVector xx(x) ;
+		NumericVector res = sapply( xx, square );
+		
+		return res ;
+	
+	', include = inc, plugin = "Rcpp" )
+	
+	checkEquals( fx(1:10) , (1:10)^2 )
+}
+



More information about the Rcpp-commits mailing list