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