[Rcpp-commits] r3460 - in pkg/Rcpp: . inst/include/Rcpp/sugar/functions inst/include/Rcpp/sugar/functions/mapply inst/include/Rcpp/traits
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Feb 3 15:20:13 CET 2012
Author: romain
Date: 2012-02-03 15:20:13 +0100 (Fri, 03 Feb 2012)
New Revision: 3460
Added:
pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply.h
pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply/
pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply/mapply_2.h
pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply/mapply_3.h
Modified:
pkg/Rcpp/ChangeLog
pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
pkg/Rcpp/inst/include/Rcpp/traits/result_of.h
Log:
introduing mapply
Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog 2012-02-03 10:00:44 UTC (rev 3459)
+++ pkg/Rcpp/ChangeLog 2012-02-03 14:20:13 UTC (rev 3460)
@@ -1,7 +1,10 @@
2012-02-03 Romain Francois <romain at r-enthusiasts.com>
* inst/include/Rcpp/Extractor.h: rmove use of Fast in Extractor
-
+
+ * inst/include/Rcpp/sugar/functions/mapply/mapply.h: new sugar function,
+ similar to sapply, but with 2 or 3 (for now) input vectors
+
2012-02-01 Romain Francois <romain at r-enthusiasts.com>
* inst/include/Rcpp/iostream/Rostream.h: no need to include Rcpp.h
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h 2012-02-03 10:00:44 UTC (rev 3459)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h 2012-02-03 14:20:13 UTC (rev 3460)
@@ -2,7 +2,7 @@
//
// functions.h: Rcpp R/C++ interface class library -- sugar functions
//
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
@@ -36,6 +36,7 @@
#include <Rcpp/sugar/functions/is_na.h>
#include <Rcpp/sugar/functions/seq_along.h>
#include <Rcpp/sugar/functions/sapply.h>
+#include <Rcpp/sugar/functions/mapply.h>
#include <Rcpp/sugar/functions/lapply.h>
#include <Rcpp/sugar/functions/ifelse.h>
#include <Rcpp/sugar/functions/pmin.h>
Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply/mapply_2.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply/mapply_2.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply/mapply_2.h 2012-02-03 14:20:13 UTC (rev 3460)
@@ -0,0 +1,73 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// mapply_2.h: Rcpp R/C++ interface class library -- mapply_2
+//
+// Copyright (C) 2012 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__mapply_2_h
+#define Rcpp__sugar__mapply_2_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE,
+ bool NA_1, typename T_1,
+ bool NA_2, typename T_2,
+ typename Function
+>
+class Mapply_2 : public VectorBase<
+ Rcpp::traits::r_sexptype_traits<
+ typename ::Rcpp::traits::result_of<Function>::type
+ >::rtype ,
+ true ,
+ Mapply_2<RTYPE,NA_1,T_1,NA_2,T_2,Function>
+> {
+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 ;
+
+ 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 ;
+
+ 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_){}
+
+ 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 EXT_1& vec_1 ;
+ const EXT_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 ) ;
+}
+
+} // Rcpp
+
+#endif
Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply/mapply_3.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply/mapply_3.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply/mapply_3.h 2012-02-03 14:20:13 UTC (rev 3460)
@@ -0,0 +1,82 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// mapply_3.h: Rcpp R/C++ interface class library -- mapply_3
+//
+// Copyright (C) 2012 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__mapply_3_h
+#define Rcpp__sugar__mapply_3_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE,
+ bool NA_1, typename T_1,
+ bool NA_2, typename T_2,
+ bool NA_3, typename T_3,
+ typename Function
+>
+class Mapply_3 : public VectorBase<
+ Rcpp::traits::r_sexptype_traits<
+ typename ::Rcpp::traits::result_of<Function>::type
+ >::rtype ,
+ true ,
+ Mapply_3<RTYPE,NA_1,T_1,NA_2,T_2,NA_3,T_3,Function>
+> {
+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 ;
+ typedef Rcpp::VectorBase<RTYPE,NA_3,T_3> VEC_3 ;
+
+ 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 ;
+ typedef typename Rcpp::traits::Extractor<RTYPE,NA_3,T_3>::type EXT_3 ;
+
+ Mapply_3( const VEC_1& vec_1_, const VEC_2& vec_2_, const VEC_3& vec_3_, Function fun_ ) :
+ vec_1(vec_1_.get_ref()), vec_2(vec_2_.get_ref()), vec_3(vec_3_.get_ref()), fun(fun_){}
+
+ inline result_type operator[]( int i ) const {
+ return fun( vec_1[i], vec_2[i], vec_3[i] );
+ }
+ inline int size() const { return vec_1.size() ; }
+
+private:
+ const EXT_1& vec_1 ;
+ const EXT_2& vec_2 ;
+ const EXT_3& vec_3 ;
+ Function fun ;
+} ;
+
+} // sugar
+
+template <int RTYPE, bool NA_1, typename T_1, bool NA_2, typename T_2, bool NA_3, typename T_3, typename Function >
+inline sugar::Mapply_3<RTYPE,NA_1,T_1,NA_2,T_2,NA_3,T_3,Function>
+mapply(
+ const Rcpp::VectorBase<RTYPE,NA_1,T_1>& t1,
+ const Rcpp::VectorBase<RTYPE,NA_2,T_2>& t2,
+ const Rcpp::VectorBase<RTYPE,NA_3,T_3>& t3,
+ Function fun
+){
+ return sugar::Mapply_3<RTYPE,NA_1,T_1,NA_2,T_2,NA_3,T_3,Function>( t1, t2, t3, fun ) ;
+}
+
+} // Rcpp
+
+#endif
Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/mapply.h 2012-02-03 14:20:13 UTC (rev 3460)
@@ -0,0 +1,28 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// mapply.h: Rcpp R/C++ interface class library -- mapply
+//
+// Copyright (C) 2012 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__mapply_h
+#define Rcpp__sugar__mapply_h
+
+#include <Rcpp/sugar/functions/mapply/mapply_3.h>
+#include <Rcpp/sugar/functions/mapply/mapply_2.h>
+
+#endif
Modified: pkg/Rcpp/inst/include/Rcpp/traits/result_of.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/result_of.h 2012-02-03 10:00:44 UTC (rev 3459)
+++ pkg/Rcpp/inst/include/Rcpp/traits/result_of.h 2012-02-03 14:20:13 UTC (rev 3460)
@@ -3,7 +3,7 @@
//
// result_of.h: Rcpp R/C++ interface class library -- traits to help wrap
//
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
@@ -41,6 +41,11 @@
typedef RESULT_TYPE type ;
} ;
+template <typename RESULT_TYPE, typename U1, typename U2, typename U3>
+struct result_of< RESULT_TYPE (*)(U1, U2, U3) >{
+ typedef RESULT_TYPE type ;
+} ;
+
}
}
More information about the Rcpp-commits
mailing list