|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- 00002 // 00003 // not.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__not_h 00023 #define Rcpp__sugar__not_h 00024 00025 namespace Rcpp{ 00026 namespace sugar{ 00027 00028 template <int RTYPE,bool NA> 00029 class not_ { 00030 public: 00031 typedef typename traits::storage_type<RTYPE>::type STORAGE ; 00032 inline int apply( STORAGE x ) const { 00033 return Rcpp::traits::is_na<RTYPE>(x) ? NA_LOGICAL : (x ? FALSE : TRUE) ; 00034 } 00035 } ; 00036 template <int RTYPE> 00037 class not_<RTYPE,false> { 00038 public: 00039 typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ; 00040 inline int apply( STORAGE x ) const { 00041 return x ? FALSE : TRUE ; 00042 } 00043 } ; 00044 template <bool NA> 00045 class not_<REALSXP,NA>{ 00046 public: 00047 inline int apply( double x ) const { 00048 return Rcpp::traits::is_na<REALSXP>( x ) ? NA_LOGICAL : ( (x == 0) ? FALSE : TRUE ) ; 00049 } 00050 } ; 00051 template <> 00052 class not_<REALSXP,false>{ 00053 public: 00054 inline int apply( double x ) const { 00055 return ( x == 0.0 ? FALSE : TRUE ) ; 00056 } 00057 } ; 00058 template <bool NA> 00059 class not_<CPLXSXP,NA>{ 00060 public: 00061 inline int apply( Rcomplex x ) const { 00062 return Rcpp::traits::is_na<CPLXSXP>( x ) ? NA_LOGICAL : ( (x.r == 0.0 & x.i == 0.0 ) ? FALSE : TRUE ) ; 00063 } 00064 } ; 00065 template <> 00066 class not_<CPLXSXP,false>{ 00067 public: 00068 inline int apply( Rcomplex x ) const { 00069 return ((x.r == 0.0) & (x.i == 0.0) ) ? FALSE : TRUE ; 00070 } 00071 } ; 00072 00073 00074 00075 template <int RTYPE, bool NA, typename T> 00076 class Not_Vector : public Rcpp::VectorBase<LGLSXP,NA, Not_Vector<RTYPE,NA,T> > { 00077 public: 00078 typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ; 00079 typedef typename traits::storage_type<RTYPE>::type STORAGE ; 00080 typedef not_<RTYPE,NA> OPERATOR ; 00081 00082 Not_Vector( const VEC_TYPE& lhs_ ) : 00083 lhs(lhs_), op() {} 00084 00085 inline STORAGE operator[]( int i ) const { 00086 return op.apply( lhs[i] ) ; 00087 } 00088 00089 inline int size() const { return lhs.size() ; } 00090 00091 private: 00092 const VEC_TYPE& lhs ; 00093 OPERATOR op ; 00094 } ; 00095 00096 } 00097 } 00098 00099 template <int RTYPE,bool NA, typename T> 00100 inline Rcpp::sugar::Not_Vector< RTYPE , NA , T > 00101 operator!( 00102 const Rcpp::VectorBase<RTYPE,NA,T>& x 00103 ) { 00104 return Rcpp::sugar::Not_Vector<RTYPE,NA, T >( x ) ; 00105 } 00106 00107 00108 #endif