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