|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 // 00003 // SugarBlock.h: Rcpp R/C++ interface class library -- sugar functions 00004 // 00005 // Copyright (C) 2010 - 2011 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_BLOCK_2_H 00023 #define RCPP_SUGAR_BLOCK_2_H 00024 00025 namespace Rcpp{ 00026 namespace sugar{ 00027 00028 template <bool NA, typename OUT, typename U1, typename T1, typename U2, typename T2> 00029 class SugarBlock_2 : public Rcpp::VectorBase< Rcpp::traits::r_sexptype_traits<OUT>::rtype , NA, SugarBlock_2<NA,OUT,U1,T1,U2,T2> > { 00030 public: 00031 typedef OUT (*FunPtr)(U1,U2) ; 00032 SugarBlock_2( FunPtr ptr_, const T1 & x_, const T2& y_ ) : 00033 ptr(ptr_), x(x_), y(y_){ 00034 // TODO: check that x and y have same size 00035 } 00036 00037 inline OUT operator[]( int i) const { 00038 return ptr( x[i], y[i] ) ; 00039 } 00040 inline int size() const { return x.size() ; } 00041 00042 private: 00043 FunPtr ptr ; 00044 const T1& x ; 00045 const T2& y ; 00046 }; 00047 00048 00049 template <bool NA, typename OUT, typename U1, typename T1, typename U2> 00050 class SugarBlock_2__VP : public Rcpp::VectorBase< Rcpp::traits::r_sexptype_traits<OUT>::rtype , NA, SugarBlock_2__VP<NA,OUT,U1,T1,U2> > { 00051 public: 00052 typedef OUT (*FunPtr)(U1,U2) ; 00053 SugarBlock_2__VP( FunPtr ptr_, const T1 & x_, U2 u2 ) : 00054 ptr(ptr_), x(x_), y(u2){} 00055 00056 inline OUT operator[]( int i) const { 00057 return ptr( x[i], y ) ; 00058 } 00059 inline int size() const { return x.size() ; } 00060 00061 private: 00062 FunPtr ptr ; 00063 const T1& x ; 00064 U2 y ; 00065 }; 00066 00067 template <bool NA, typename OUT, typename U1, typename U2, typename T2> 00068 class SugarBlock_2__PV : public Rcpp::VectorBase< Rcpp::traits::r_sexptype_traits<OUT>::rtype , NA, SugarBlock_2__PV<NA,OUT,U1,U2,T2> > { 00069 public: 00070 typedef OUT (*FunPtr)(U1,U2) ; 00071 SugarBlock_2__PV( FunPtr ptr_, U1 u1, const T2& y_ ) : 00072 ptr(ptr_), x(u1), y(y_){} 00073 00074 inline OUT operator[]( int i) const { 00075 return ptr( x, y[i] ) ; 00076 } 00077 inline int size() const { return y.size() ; } 00078 00079 private: 00080 FunPtr ptr ; 00081 U1 x ; 00082 const T2& y ; 00083 }; 00084 00085 00086 } // sugar 00087 } // Rcpp 00088 00089 #define SB2_LHT VectorBase<REALSXP,LHS_NA,LHS_T> 00090 #define SB2_RHT VectorBase<REALSXP,RHS_NA,RHS_T> 00091 00092 #define SUGAR_BLOCK_2(__NAME__,__SYMBOL__) \ 00093 namespace Rcpp{ \ 00094 template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T > \ 00095 inline sugar::SugarBlock_2< (LHS_NA||RHS_NA) ,double,double,SB2_LHT,double,SB2_RHT> \ 00096 __NAME__( \ 00097 const SB2_LHT& lhs, \ 00098 const SB2_RHT& rhs \ 00099 ){ \ 00100 return sugar::SugarBlock_2< (LHS_NA||RHS_NA) ,double,double,SB2_LHT,double,SB2_RHT >(\ 00101 __SYMBOL__ , lhs, rhs \ 00102 ) ; \ 00103 } \ 00104 template <bool LHS_NA, typename LHS_T> \ 00105 inline sugar::SugarBlock_2__VP<LHS_NA,double,double,SB2_LHT,double> \ 00106 __NAME__( \ 00107 const SB2_LHT& lhs, \ 00108 double rhs \ 00109 ){ \ 00110 return sugar::SugarBlock_2__VP<LHS_NA,double,double,SB2_LHT,double>( \ 00111 __SYMBOL__ , lhs, rhs \ 00112 ) ; \ 00113 } \ 00114 template <bool RHS_NA, typename RHS_T> \ 00115 inline sugar::SugarBlock_2__PV<RHS_NA,double,double,double,SB2_RHT> \ 00116 __NAME__( \ 00117 double lhs, \ 00118 const SB2_RHT& rhs \ 00119 ){ \ 00120 return sugar::SugarBlock_2__PV<RHS_NA,double,double,double,SB2_RHT >( \ 00121 __SYMBOL__ , lhs, rhs \ 00122 ) ; \ 00123 } \ 00124 } 00125 00126 00127 #define SUGAR_BLOCK_2_NA(__NAME__,__SYMBOL__,__NA__) \ 00128 namespace Rcpp{ \ 00129 template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T > \ 00130 inline sugar::SugarBlock_2< __NA__ ,double,double,SB2_LHT,double,SB2_RHT> \ 00131 __NAME__( \ 00132 const SB2_LHT& lhs, \ 00133 const SB2_RHT& rhs \ 00134 ){ \ 00135 return sugar::SugarBlock_2< __NA__ ,double,double,SB2_LHT,double,SB2_RHT \ 00136 >( \ 00137 __SYMBOL__ , lhs, rhs \ 00138 ) ; \ 00139 } \ 00140 template <bool LHS_NA, typename LHS_T> \ 00141 inline sugar::SugarBlock_2__VP<__NA__,double,double,SB2_LHT,double> \ 00142 __NAME__( \ 00143 const SB2_LHT& lhs, \ 00144 double rhs \ 00145 ){ \ 00146 return sugar::SugarBlock_2__VP<__NA__,double,double,SB2_LHT,double>( \ 00147 __SYMBOL__ , lhs, rhs \ 00148 ) ; \ 00149 } \ 00150 template <bool RHS_NA, typename RHS_T> \ 00151 inline sugar::SugarBlock_2__PV<__NA__,double,double,double,SB2_RHT> \ 00152 __NAME__( \ 00153 double lhs, \ 00154 const SB2_RHT& rhs \ 00155 ){ \ 00156 return sugar::SugarBlock_2__PV<__NA__,double,double,double,SB2_RHT \ 00157 >( \ 00158 __SYMBOL__ , lhs, rhs \ 00159 ) ; \ 00160 } \ 00161 } 00162 00163 00164 #endif