Rcpp Version 0.12.12
Comparator.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // LessThan.h: Rcpp R/C++ interface class library -- vector operators
4 //
5 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp__sugar__Comparator_h
23 #define Rcpp__sugar__Comparator_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <int RTYPE, typename Operator, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
29 class Comparator :
30  public ::Rcpp::VectorBase< LGLSXP, true, Comparator<RTYPE,Operator,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
31 
32 public:
36 
37  Comparator( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_) :
38  lhs(lhs_), rhs(rhs_), op() {}
39 
40  inline int operator[]( R_xlen_t i ) const {
41  STORAGE x = lhs[i] ;
42  if( Rcpp::traits::is_na<RTYPE>( x ) ) return NA_LOGICAL ;
43  STORAGE y = rhs[i] ;
44  if( Rcpp::traits::is_na<RTYPE>( y ) ) return NA_LOGICAL ;
45  return op( x, y ) ;
46  }
47 
48  inline R_xlen_t size() const { return lhs.size() ; }
49 
50 private:
51  const LHS_TYPE& lhs ;
52  const RHS_TYPE& rhs ;
53  Operator op ;
54 
55 } ;
56 
57 
58 
59 template <int RTYPE, typename Operator, typename LHS_T, bool RHS_NA, typename RHS_T>
60 class Comparator<RTYPE,Operator,false,LHS_T,RHS_NA,RHS_T> :
61  public ::Rcpp::VectorBase< LGLSXP, true, Comparator<RTYPE,Operator,false,LHS_T,RHS_NA,RHS_T> > {
62 
63 public:
67 
68  Comparator( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_) :
69  lhs(lhs_), rhs(rhs_), op() {}
70 
71  inline int operator[]( R_xlen_t i ) const {
72  STORAGE y = rhs[i] ;
73  if( Rcpp::traits::is_na<RTYPE>( y ) ) return NA_LOGICAL ;
74  return op( lhs[i], y ) ;
75  }
76 
77  inline R_xlen_t size() const { return lhs.size() ; }
78 
79 private:
80  const LHS_TYPE& lhs ;
81  const RHS_TYPE& rhs ;
82  Operator op ;
83 
84 } ;
85 
86 
87 template <int RTYPE, typename Operator, typename LHS_T, typename RHS_T>
88 class Comparator<RTYPE,Operator,false,LHS_T,false,RHS_T> :
89  public ::Rcpp::VectorBase< LGLSXP, true, Comparator<RTYPE,Operator,false,LHS_T,false,RHS_T> > {
90 
91 public:
95 
96  Comparator( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_) :
97  lhs(lhs_), rhs(rhs_), op() {}
98 
99  inline int operator[]( R_xlen_t i ) const {
100  return op( lhs[i], rhs[i] ) ;
101  }
102 
103  inline R_xlen_t size() const { return lhs.size() ; }
104 
105 private:
106  const LHS_TYPE& lhs ;
107  const RHS_TYPE& rhs ;
108  Operator op ;
109 
110 } ;
111 
112 
113 }
114 }
115 
116 
117 #endif
traits::storage_type< RTYPE >::type STORAGE
Definition: Comparator.h:35
R_xlen_t size() const
Definition: VectorBase.h:49
R_xlen_t size() const
Definition: Comparator.h:48
const RHS_TYPE & rhs
Definition: Comparator.h:52
Comparator(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: Comparator.h:37
Rcpp::VectorBase< RTYPE, LHS_NA, LHS_T > LHS_TYPE
Definition: Comparator.h:33
Comparator(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: Comparator.h:96
const LHS_TYPE & lhs
Definition: Comparator.h:51
Rcpp::VectorBase< RTYPE, RHS_NA, RHS_T > RHS_TYPE
Definition: Comparator.h:34
Comparator(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: Comparator.h:68
Rcpp API.
Definition: algo.h:28
int operator[](R_xlen_t i) const
Definition: Comparator.h:40