|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- 00002 // 00003 // divides.h: Rcpp R/C++ interface class library -- operator- 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__divides_h 00023 #define Rcpp__sugar__divides_h 00024 00025 namespace Rcpp{ 00026 namespace sugar{ 00027 00028 template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T > 00029 class Divides_Vector_Vector : public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T> > { 00030 public: 00031 typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ; 00032 typedef typename Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ; 00033 typedef typename traits::storage_type<RTYPE>::type STORAGE ; 00034 typedef typename Rcpp::traits::Extractor< RTYPE, LHS_NA, LHS_T>::type LHS_EXT ; 00035 typedef typename Rcpp::traits::Extractor< RTYPE, RHS_NA, RHS_T>::type RHS_EXT ; 00036 00037 Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 00038 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {} 00039 00040 inline STORAGE operator[]( int i ) const { 00041 STORAGE x = lhs[i] ; 00042 if( Rcpp::traits::is_na<RTYPE>( x ) ) return x ; 00043 STORAGE y = rhs[i] ; 00044 return Rcpp::traits::is_na<RTYPE>( y ) ? y : ( x / y ) ; 00045 } 00046 00047 inline int size() const { return lhs.size() ; } 00048 00049 private: 00050 const LHS_EXT& lhs ; 00051 const RHS_EXT& rhs ; 00052 } ; 00053 // RTYPE = REALSXP 00054 template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T > 00055 class Divides_Vector_Vector<REALSXP,LHS_NA,LHS_T,RHS_NA,RHS_T> : 00056 public Rcpp::VectorBase<REALSXP,true, Divides_Vector_Vector<REALSXP,LHS_NA,LHS_T,RHS_NA,RHS_T> > { 00057 public: 00058 typedef typename Rcpp::VectorBase<REALSXP,LHS_NA,LHS_T> LHS_TYPE ; 00059 typedef typename Rcpp::VectorBase<REALSXP,RHS_NA,RHS_T> RHS_TYPE ; 00060 typedef typename Rcpp::traits::Extractor<REALSXP, LHS_NA, LHS_T>::type LHS_EXT ; 00061 typedef typename Rcpp::traits::Extractor<REALSXP, RHS_NA, RHS_T>::type RHS_EXT ; 00062 00063 Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 00064 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {} 00065 00066 inline double operator[]( int i ) const { 00067 return lhs[i] / rhs[i] ; 00068 } 00069 00070 inline int size() const { return lhs.size() ; } 00071 00072 private: 00073 const LHS_EXT& lhs ; 00074 const RHS_EXT& rhs ; 00075 } ; 00076 00077 00078 template <int RTYPE, typename LHS_T, bool RHS_NA, typename RHS_T > 00079 class Divides_Vector_Vector<RTYPE,false,LHS_T,RHS_NA,RHS_T> : public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Vector<RTYPE,false,LHS_T,RHS_NA,RHS_T> > { 00080 public: 00081 typedef typename Rcpp::VectorBase<RTYPE,false,LHS_T> LHS_TYPE ; 00082 typedef typename Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ; 00083 typedef typename traits::storage_type<RTYPE>::type STORAGE ; 00084 typedef typename Rcpp::traits::Extractor< RTYPE, false, LHS_T>::type LHS_EXT ; 00085 typedef typename Rcpp::traits::Extractor< RTYPE, RHS_NA, RHS_T>::type RHS_EXT ; 00086 00087 Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 00088 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {} 00089 00090 inline STORAGE operator[]( int i ) const { 00091 STORAGE y = rhs[i] ; 00092 if( Rcpp::traits::is_na<RTYPE>( y ) ) return y ; 00093 return lhs[i] / y ; 00094 } 00095 00096 inline int size() const { return lhs.size() ; } 00097 00098 private: 00099 const LHS_EXT& lhs ; 00100 const RHS_EXT& rhs ; 00101 } ; 00102 // RTYPE = REALSXP 00103 template <typename LHS_T, bool RHS_NA, typename RHS_T > 00104 class Divides_Vector_Vector<REALSXP,false,LHS_T,RHS_NA,RHS_T> : 00105 public Rcpp::VectorBase<REALSXP,true, Divides_Vector_Vector<REALSXP,false,LHS_T,RHS_NA,RHS_T> > { 00106 public: 00107 typedef typename Rcpp::VectorBase<REALSXP,false,LHS_T> LHS_TYPE ; 00108 typedef typename Rcpp::VectorBase<REALSXP,RHS_NA,RHS_T> RHS_TYPE ; 00109 typedef typename Rcpp::traits::Extractor<REALSXP, false, LHS_T>::type LHS_EXT ; 00110 typedef typename Rcpp::traits::Extractor<REALSXP, RHS_NA, RHS_T>::type RHS_EXT ; 00111 00112 Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 00113 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {} 00114 00115 inline double operator[]( int i ) const { 00116 return lhs[i] / rhs[i] ; 00117 } 00118 00119 inline int size() const { return lhs.size() ; } 00120 00121 private: 00122 const LHS_EXT& lhs ; 00123 const RHS_EXT& rhs ; 00124 } ; 00125 00126 00127 template <int RTYPE, bool LHS_NA, typename LHS_T, typename RHS_T > 00128 class Divides_Vector_Vector<RTYPE,LHS_NA,LHS_T,false,RHS_T> : 00129 public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Vector<RTYPE,LHS_NA,LHS_T,false,RHS_T> > { 00130 public: 00131 typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ; 00132 typedef typename Rcpp::VectorBase<RTYPE,false,RHS_T> RHS_TYPE ; 00133 typedef typename traits::storage_type<RTYPE>::type STORAGE ; 00134 typedef typename Rcpp::traits::Extractor< RTYPE, LHS_NA, LHS_T>::type LHS_EXT ; 00135 typedef typename Rcpp::traits::Extractor< RTYPE, false, RHS_T>::type RHS_EXT ; 00136 00137 Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 00138 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {} 00139 00140 inline STORAGE operator[]( int i ) const { 00141 STORAGE x = lhs[i] ; 00142 if( Rcpp::traits::is_na<RTYPE>( x ) ) return x ; 00143 return x / rhs[i] ; 00144 } 00145 inline int size() const { return lhs.size() ; } 00146 00147 private: 00148 const LHS_EXT& lhs ; 00149 const RHS_EXT& rhs ; 00150 } ; 00151 // RTYPE = REALSXP 00152 template <bool LHS_NA, typename LHS_T, typename RHS_T > 00153 class Divides_Vector_Vector<REALSXP,LHS_NA,LHS_T,false,RHS_T> : 00154 public Rcpp::VectorBase<REALSXP,true, Divides_Vector_Vector<REALSXP,LHS_NA,LHS_T,false,RHS_T> > { 00155 public: 00156 typedef typename Rcpp::VectorBase<REALSXP,LHS_NA,LHS_T> LHS_TYPE ; 00157 typedef typename Rcpp::VectorBase<REALSXP,false,RHS_T> RHS_TYPE ; 00158 typedef typename Rcpp::traits::Extractor<REALSXP, LHS_NA, LHS_T>::type LHS_EXT ; 00159 typedef typename Rcpp::traits::Extractor<REALSXP, false, RHS_T>::type RHS_EXT ; 00160 00161 Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 00162 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {} 00163 00164 inline double operator[]( int i ) const { 00165 return lhs[i] / rhs[i] ; 00166 } 00167 inline int size() const { return lhs.size() ; } 00168 00169 private: 00170 const LHS_EXT& lhs ; 00171 const RHS_EXT& rhs ; 00172 } ; 00173 00174 00175 template <int RTYPE, typename LHS_T, typename RHS_T > 00176 class Divides_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> : 00177 public Rcpp::VectorBase<RTYPE,false, Divides_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> > { 00178 public: 00179 typedef typename Rcpp::VectorBase<RTYPE,false,LHS_T> LHS_TYPE ; 00180 typedef typename Rcpp::VectorBase<RTYPE,false,RHS_T> RHS_TYPE ; 00181 typedef typename traits::storage_type<RTYPE>::type STORAGE ; 00182 typedef typename Rcpp::traits::Extractor<RTYPE, false, LHS_T>::type LHS_EXT ; 00183 typedef typename Rcpp::traits::Extractor<RTYPE, false, RHS_T>::type RHS_EXT ; 00184 00185 Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 00186 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {} 00187 00188 inline STORAGE operator[]( int i ) const { 00189 return lhs[i] / rhs[i] ; 00190 } 00191 00192 inline int size() const { return lhs.size() ; } 00193 00194 private: 00195 const LHS_EXT& lhs ; 00196 const RHS_EXT& rhs ; 00197 } ; 00198 // RTYPE : REALSXP 00199 template <typename LHS_T, typename RHS_T > 00200 class Divides_Vector_Vector<REALSXP,false,LHS_T,false,RHS_T> : 00201 public Rcpp::VectorBase<REALSXP,false, Divides_Vector_Vector<REALSXP,false,LHS_T,false,RHS_T> > { 00202 public: 00203 typedef typename Rcpp::VectorBase<REALSXP,false,LHS_T> LHS_TYPE ; 00204 typedef typename Rcpp::VectorBase<REALSXP,false,RHS_T> RHS_TYPE ; 00205 typedef typename Rcpp::traits::Extractor<REALSXP, false, LHS_T>::type LHS_EXT ; 00206 typedef typename Rcpp::traits::Extractor<REALSXP, false, RHS_T>::type RHS_EXT ; 00207 00208 Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 00209 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {} 00210 00211 inline double operator[]( int i ) const { 00212 return lhs[i] / rhs[i] ; 00213 } 00214 00215 inline int size() const { return lhs.size() ; } 00216 00217 private: 00218 const LHS_EXT& lhs ; 00219 const RHS_EXT& rhs ; 00220 } ; 00221 00222 00223 00224 00225 template <int RTYPE, bool NA, typename T> 00226 class Divides_Vector_Primitive : 00227 public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Primitive<RTYPE,NA,T> > { 00228 public: 00229 typedef typename traits::storage_type<RTYPE>::type STORAGE ; 00230 typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ; 00231 typedef typename Rcpp::traits::Extractor<RTYPE,NA,T>::type VEC_EXT ; 00232 00233 Divides_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 00234 lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) ) { 00235 } 00236 00237 inline STORAGE operator[]( int i ) const { 00238 if(rhs_na) return rhs ; 00239 STORAGE x = lhs[i] ; 00240 return Rcpp::traits::is_na<RTYPE>(x) ? x : (x / rhs) ; 00241 } 00242 00243 inline int size() const { return lhs.size() ; } 00244 00245 private: 00246 const VEC_EXT& lhs ; 00247 STORAGE rhs ; 00248 bool rhs_na ; 00249 } ; 00250 // RTYPE : REALSXP 00251 template <bool NA, typename T> 00252 class Divides_Vector_Primitive<REALSXP,NA,T> : 00253 public Rcpp::VectorBase<REALSXP,true, Divides_Vector_Primitive<REALSXP,NA,T> > { 00254 public: 00255 typedef typename Rcpp::VectorBase<REALSXP,NA,T> VEC_TYPE ; 00256 typedef typename Rcpp::traits::Extractor<REALSXP,NA,T>::type VEC_EXT ; 00257 00258 Divides_Vector_Primitive( const VEC_TYPE& lhs_, double rhs_ ) : 00259 lhs(lhs_.get_ref()), rhs(rhs_) { 00260 } 00261 00262 inline double operator[]( int i ) const { 00263 return lhs[i] / rhs ; 00264 } 00265 00266 inline int size() const { return lhs.size() ; } 00267 00268 private: 00269 const VEC_EXT& lhs ; 00270 double rhs ; 00271 } ; 00272 00273 00274 00275 template <int RTYPE, typename T> 00276 class Divides_Vector_Primitive<RTYPE,false,T> : 00277 public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Primitive<RTYPE,false,T> > { 00278 public: 00279 typedef typename traits::storage_type<RTYPE>::type STORAGE ; 00280 typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ; 00281 typedef typename Rcpp::traits::Extractor<RTYPE,false,T>::type VEC_EXT ; 00282 00283 Divides_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 00284 lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) ) {} 00285 00286 inline STORAGE operator[]( int i ) const { 00287 if( rhs_na ) return rhs ; 00288 STORAGE x = lhs[i] ; 00289 return Rcpp::traits::is_na<RTYPE>(x) ? x : (x / rhs) ; 00290 } 00291 inline int size() const { return lhs.size() ; } 00292 00293 private: 00294 const VEC_EXT& lhs ; 00295 STORAGE rhs ; 00296 bool rhs_na ; 00297 } ; 00298 // RTYPE = REALSXP 00299 template <typename T> 00300 class Divides_Vector_Primitive<REALSXP,false,T> : 00301 public Rcpp::VectorBase<REALSXP,true, Divides_Vector_Primitive<REALSXP,false,T> > { 00302 public: 00303 typedef typename Rcpp::VectorBase<REALSXP,false,T> VEC_TYPE ; 00304 typedef typename Rcpp::traits::Extractor<REALSXP,false,T>::type VEC_EXT ; 00305 00306 Divides_Vector_Primitive( const VEC_TYPE& lhs_, double rhs_ ) : 00307 lhs(lhs_), rhs(rhs_){} 00308 00309 inline double operator[]( int i ) const { 00310 return lhs[i] / rhs ; 00311 } 00312 inline int size() const { return lhs.size() ; } 00313 00314 private: 00315 const VEC_EXT& lhs ; 00316 double rhs ; 00317 } ; 00318 00319 00320 00321 template <int RTYPE, bool NA, typename T> 00322 class Divides_Primitive_Vector : 00323 public Rcpp::VectorBase<RTYPE,true, Divides_Primitive_Vector<RTYPE,NA,T> > { 00324 public: 00325 typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ; 00326 typedef typename Rcpp::traits::Extractor<RTYPE,NA,T>::type VEC_EXT ; 00327 typedef typename traits::storage_type<RTYPE>::type STORAGE ; 00328 00329 Divides_Primitive_Vector( STORAGE lhs_, const VEC_TYPE& rhs_ ) : 00330 lhs(lhs_), rhs(rhs_.get_ref()), lhs_na( Rcpp::traits::is_na<RTYPE>(lhs_) ) {} 00331 00332 inline STORAGE operator[]( int i ) const { 00333 if( lhs_na ) return lhs ; 00334 STORAGE x = rhs[i] ; 00335 return Rcpp::traits::is_na<RTYPE>(x) ? x : (lhs / x) ; 00336 } 00337 inline int size() const { return rhs.size() ; } 00338 private: 00339 STORAGE lhs ; 00340 const VEC_EXT& rhs ; 00341 bool lhs_na ; 00342 } ; 00343 // RTYPE = REALSXP 00344 template <bool NA, typename T> 00345 class Divides_Primitive_Vector<REALSXP,NA,T> : 00346 public Rcpp::VectorBase<REALSXP,true, Divides_Primitive_Vector<REALSXP,NA,T> > { 00347 public: 00348 typedef typename Rcpp::VectorBase<REALSXP,NA,T> VEC_TYPE ; 00349 typedef typename Rcpp::traits::Extractor<REALSXP,NA,T>::type VEC_EXT ; 00350 00351 Divides_Primitive_Vector( double lhs_, const VEC_TYPE& rhs_ ) : 00352 lhs(lhs_), rhs(rhs_.get_ref()) {} 00353 00354 inline double operator[]( int i ) const { 00355 return lhs / rhs[i] ; 00356 } 00357 inline int size() const { return rhs.size() ; } 00358 private: 00359 double lhs ; 00360 const VEC_EXT& rhs ; 00361 } ; 00362 00363 00364 00365 template <int RTYPE, typename T> 00366 class Divides_Primitive_Vector<RTYPE,false,T> : 00367 public Rcpp::VectorBase<RTYPE,true, Divides_Primitive_Vector<RTYPE,false,T> > { 00368 public: 00369 typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ; 00370 typedef typename traits::storage_type<RTYPE>::type STORAGE ; 00371 typedef typename Rcpp::traits::Extractor<RTYPE,false,T>::type VEC_EXT ; 00372 00373 Divides_Primitive_Vector( STORAGE lhs_, const VEC_TYPE& rhs_ ) : 00374 lhs(lhs_), rhs(rhs_.get_ref()), lhs_na( Rcpp::traits::is_na<RTYPE>(lhs_) ) {} 00375 00376 inline STORAGE operator[]( int i ) const { 00377 if( lhs_na ) return lhs ; 00378 return lhs / rhs[i] ; 00379 } 00380 inline int size() const { return rhs.size() ; } 00381 00382 private: 00383 STORAGE lhs ; 00384 const VEC_EXT& rhs ; 00385 bool lhs_na ; 00386 } ; 00387 // RTYPE = REALSXP 00388 template <typename T> 00389 class Divides_Primitive_Vector<REALSXP,false,T> : 00390 public Rcpp::VectorBase<REALSXP,true, Divides_Primitive_Vector<REALSXP,false,T> > { 00391 public: 00392 typedef typename Rcpp::VectorBase<REALSXP,false,T> VEC_TYPE ; 00393 typedef typename Rcpp::traits::Extractor<REALSXP,false,T>::type VEC_EXT ; 00394 00395 Divides_Primitive_Vector( double lhs_, const VEC_TYPE& rhs_ ) : 00396 lhs(lhs_), rhs(rhs_.get_ref()) {} 00397 00398 inline double operator[]( int i ) const { 00399 return lhs / rhs[i] ; 00400 } 00401 inline int size() const { return rhs.size() ; } 00402 00403 private: 00404 double lhs ; 00405 const VEC_EXT& rhs ; 00406 } ; 00407 00408 00409 } 00410 } 00411 00412 template <int RTYPE,bool NA, typename T> 00413 inline Rcpp::sugar::Divides_Vector_Primitive< RTYPE , NA, T > 00414 operator/( 00415 const Rcpp::VectorBase<RTYPE,NA,T>& lhs, 00416 typename Rcpp::traits::storage_type<RTYPE>::type rhs 00417 ) { 00418 return Rcpp::sugar::Divides_Vector_Primitive<RTYPE,NA,T>( lhs, rhs ) ; 00419 } 00420 00421 00422 template <int RTYPE,bool NA, typename T> 00423 inline Rcpp::sugar::Divides_Primitive_Vector< RTYPE , NA,T> 00424 operator/( 00425 typename Rcpp::traits::storage_type<RTYPE>::type lhs, 00426 const Rcpp::VectorBase<RTYPE,NA,T>& rhs 00427 ) { 00428 return Rcpp::sugar::Divides_Primitive_Vector<RTYPE,NA,T>( lhs, rhs ) ; 00429 } 00430 00431 template <int RTYPE,bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T> 00432 inline Rcpp::sugar::Divides_Vector_Vector< 00433 RTYPE , 00434 LHS_NA, LHS_T, 00435 RHS_NA, RHS_T 00436 > 00437 operator/( 00438 const Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>& lhs, 00439 const Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>& rhs 00440 ) { 00441 return Rcpp::sugar::Divides_Vector_Vector< 00442 RTYPE, 00443 LHS_NA,LHS_T, 00444 RHS_NA,RHS_T 00445 >( lhs, rhs ) ; 00446 } 00447 00448 #endif