Rcpp Version 0.9.10
Comparator.h
Go to the documentation of this file.
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_h
00023 #define Rcpp__sugar__Comparator_h
00024 
00025 namespace Rcpp{
00026 namespace sugar{
00027 
00028 template <int RTYPE, typename Operator, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
00029 class Comparator : 
00030         public ::Rcpp::VectorBase< LGLSXP, true, Comparator<RTYPE,Operator,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
00031 
00032 public:
00033         typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
00034         typedef typename Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
00035         typedef typename traits::storage_type<RTYPE>::type STORAGE ;
00036         
00037         Comparator( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_) : 
00038                 lhs(lhs_), rhs(rhs_), op() {}
00039         
00040         inline int operator[]( int i ) const {
00041                 STORAGE x = lhs[i] ;
00042                 if( Rcpp::traits::is_na<RTYPE>( x ) ) return NA_LOGICAL ;
00043                 STORAGE y = rhs[i] ;
00044                 if( Rcpp::traits::is_na<RTYPE>( y ) ) return NA_LOGICAL ;
00045                 return op( x, y ) ;
00046         }
00047         
00048         inline int size() const { return lhs.size() ; }
00049         
00050 private:
00051         const LHS_TYPE& lhs ;
00052         const RHS_TYPE& rhs ;
00053         Operator op ;
00054         
00055 } ;
00056 
00057 
00058 
00059 template <int RTYPE, typename Operator, typename LHS_T, bool RHS_NA, typename RHS_T>
00060 class Comparator<RTYPE,Operator,false,LHS_T,RHS_NA,RHS_T> : 
00061         public ::Rcpp::VectorBase< LGLSXP, true, Comparator<RTYPE,Operator,false,LHS_T,RHS_NA,RHS_T> > {
00062 
00063 public:
00064         typedef typename Rcpp::VectorBase<RTYPE,false,LHS_T> LHS_TYPE ;
00065         typedef typename Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
00066         typedef typename traits::storage_type<RTYPE>::type STORAGE ;
00067         
00068         Comparator( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_) : 
00069                 lhs(lhs_), rhs(rhs_), op() {}
00070         
00071         inline int operator[]( int i ) const {
00072                 STORAGE y = rhs[i] ;
00073                 if( Rcpp::traits::is_na<RTYPE>( y ) ) return NA_LOGICAL ;
00074                 return op( lhs[i], y ) ;
00075         }
00076         
00077         inline int size() const { return lhs.size() ; }
00078         
00079 private:
00080         const LHS_TYPE& lhs ;
00081         const RHS_TYPE& rhs ;
00082         Operator op ;
00083         
00084 } ;
00085 
00086 
00087 template <int RTYPE, typename Operator, typename LHS_T, typename RHS_T>
00088 class Comparator<RTYPE,Operator,false,LHS_T,false,RHS_T> : 
00089         public ::Rcpp::VectorBase< LGLSXP, true, Comparator<RTYPE,Operator,false,LHS_T,false,RHS_T> > {
00090 
00091 public:
00092         typedef typename Rcpp::VectorBase<RTYPE,false,LHS_T> LHS_TYPE ;
00093         typedef typename Rcpp::VectorBase<RTYPE,false,RHS_T> RHS_TYPE ;
00094         typedef typename traits::storage_type<RTYPE>::type STORAGE ;
00095         
00096         Comparator( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_) : 
00097                 lhs(lhs_), rhs(rhs_), op() {}
00098         
00099         inline int operator[]( int i ) const {
00100                 return op( lhs[i], rhs[i] ) ;
00101         }
00102         
00103         inline int size() const { return lhs.size() ; }
00104         
00105 private:
00106         const LHS_TYPE& lhs ;
00107         const RHS_TYPE& rhs ;
00108         Operator op ;
00109         
00110 } ;
00111 
00112 
00113 }
00114 }
00115 
00116 
00117 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines