|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- 00002 // 00003 // mapply_2.h: Rcpp R/C++ interface class library -- mapply_2 00004 // 00005 // Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois 00006 // 00007 // This file is part of Rcpp. 00008 // 00009 // Rcpp is free software: you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation, either version 2 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // Rcpp is distributed in the hope that it will be useful, but 00015 // WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00021 00022 #ifndef Rcpp__sugar__mapply_2_h 00023 #define Rcpp__sugar__mapply_2_h 00024 00025 namespace Rcpp{ 00026 namespace sugar{ 00027 00028 template <int RTYPE, 00029 bool NA_1, typename T_1, 00030 bool NA_2, typename T_2, 00031 typename Function 00032 > 00033 class Mapply_2 : public VectorBase< 00034 Rcpp::traits::r_sexptype_traits< 00035 typename ::Rcpp::traits::result_of<Function>::type 00036 >::rtype , 00037 true , 00038 Mapply_2<RTYPE,NA_1,T_1,NA_2,T_2,Function> 00039 > { 00040 public: 00041 typedef typename ::Rcpp::traits::result_of<Function>::type result_type ; 00042 00043 typedef Rcpp::VectorBase<RTYPE,NA_1,T_1> VEC_1 ; 00044 typedef Rcpp::VectorBase<RTYPE,NA_2,T_2> VEC_2 ; 00045 00046 typedef typename Rcpp::traits::Extractor<RTYPE,NA_1,T_1>::type EXT_1 ; 00047 typedef typename Rcpp::traits::Extractor<RTYPE,NA_2,T_2>::type EXT_2 ; 00048 00049 Mapply_2( const VEC_1& vec_1_, const VEC_2& vec_2_, Function fun_ ) : 00050 vec_1(vec_1_.get_ref()), vec_2(vec_2_.get_ref()), fun(fun_){} 00051 00052 inline result_type operator[]( int i ) const { 00053 return fun( vec_1[i], vec_2[i] ); 00054 } 00055 inline int size() const { return vec_1.size() ; } 00056 00057 private: 00058 const EXT_1& vec_1 ; 00059 const EXT_2& vec_2 ; 00060 Function fun ; 00061 } ; 00062 00063 } // sugar 00064 00065 template <int RTYPE, bool NA_1, typename T_1, bool NA_2, typename T_2, typename Function > 00066 inline sugar::Mapply_2<RTYPE,NA_1,T_1,NA_2,T_2,Function> 00067 mapply( const Rcpp::VectorBase<RTYPE,NA_1,T_1>& t1, const Rcpp::VectorBase<RTYPE,NA_2,T_2>& t2, Function fun ){ 00068 return sugar::Mapply_2<RTYPE,NA_1,T_1,NA_2,T_2,Function>( t1, t2, fun ) ; 00069 } 00070 00071 } // Rcpp 00072 00073 #endif