|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- 00002 // 00003 // unary_minus.h: Rcpp R/C++ interface class library -- unary 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__unary_minus_h 00023 #define Rcpp__sugar__unary_minus_h 00024 00025 namespace Rcpp{ 00026 namespace sugar{ 00027 00028 template <int RTYPE> 00029 struct unary_minus_result_type{ 00030 typedef typename traits::storage_type<RTYPE>::type type ; 00031 enum{ value = RTYPE } ; 00032 } ; 00033 template <> 00034 struct unary_minus_result_type<LGLSXP>{ 00035 typedef traits::storage_type<INTSXP>::type type ; 00036 enum{ value = INTSXP } ; 00037 } ; 00038 00039 00040 template <int RTYPE,bool NA> 00041 class unary_minus { 00042 public: 00043 typedef typename traits::storage_type<RTYPE>::type STORAGE ; 00044 typedef typename unary_minus_result_type<RTYPE>::type RESULT ; 00045 inline RESULT apply( STORAGE x ) const { 00046 return Rcpp::traits::is_na<RTYPE>(x) ? x : ( -x ) ; 00047 } 00048 } ; 00049 template <int RTYPE> 00050 class unary_minus<RTYPE,false> { 00051 public: 00052 typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ; 00053 typedef typename unary_minus_result_type<RTYPE>::type RESULT ; 00054 inline RESULT apply( STORAGE x ) const { 00055 return -x ; 00056 } 00057 } ; 00058 template <bool NA> 00059 class unary_minus<CPLXSXP,NA>{ 00060 public: 00061 inline Rcomplex apply( Rcomplex x ) const { 00062 if (Rcpp::traits::is_na<CPLXSXP>( x ) ) return x; 00063 00064 Rcomplex cx ; 00065 cx.r = -x.r; 00066 cx.i = -x.i ; 00067 return cx ; 00068 } 00069 } ; 00070 template <> 00071 class unary_minus<CPLXSXP,false>{ 00072 public: 00073 inline Rcomplex apply( Rcomplex x ) const { 00074 Rcomplex cx ; 00075 cx.r = -x.r; 00076 cx.i = -x.i ; 00077 return cx ; 00078 } 00079 } ; 00080 00081 00082 template <int RTYPE, bool NA, typename T> 00083 class UnaryMinus_Vector : public Rcpp::VectorBase< 00084 unary_minus_result_type<RTYPE>::value , 00085 NA, 00086 UnaryMinus_Vector< unary_minus_result_type<RTYPE>::value ,NA,T> 00087 > { 00088 public: 00089 typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ; 00090 typedef typename traits::storage_type<RTYPE>::type STORAGE ; 00091 typedef typename unary_minus_result_type<RTYPE>::type RESULT ; 00092 typedef unary_minus<RTYPE,NA> OPERATOR ; 00093 00094 UnaryMinus_Vector( const VEC_TYPE& lhs_ ) : 00095 lhs(lhs_), op() {} 00096 00097 inline RESULT operator[]( int i ) const { 00098 return op.apply( lhs[i] ) ; 00099 } 00100 00101 inline int size() const { return lhs.size() ; } 00102 00103 private: 00104 const VEC_TYPE& lhs ; 00105 OPERATOR op ; 00106 } ; 00107 00108 } 00109 } 00110 00111 template <int RTYPE,bool NA, typename T> 00112 inline Rcpp::sugar::UnaryMinus_Vector< RTYPE , NA , T > 00113 operator-( 00114 const Rcpp::VectorBase<RTYPE,NA,T>& x 00115 ) { 00116 return Rcpp::sugar::UnaryMinus_Vector<RTYPE,NA, T >( x ) ; 00117 } 00118 00119 00120 #endif