Rcpp Version 0.12.12
Comparator_With_One_Value.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_With_One_Value_h
23 #define Rcpp__sugar__Comparator_With_One_Value_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <int RTYPE, typename Operator, bool NA, typename T>
29 class Comparator_With_One_Value : public ::Rcpp::VectorBase< LGLSXP, true, Comparator_With_One_Value<RTYPE,Operator,NA,T> > {
30 public:
33  typedef int (Comparator_With_One_Value::*METHOD)(int) const ;
34 
35  Comparator_With_One_Value( const VEC_TYPE& lhs_, STORAGE rhs_ ) :
36  lhs(lhs_), rhs(rhs_), m(), op() {
37 
38  m = Rcpp::traits::is_na<RTYPE>(rhs) ?
41 
42  }
43 
44  inline int operator[]( R_xlen_t i ) const {
45  return (this->*m)(i) ;
46  }
47 
48  inline R_xlen_t size() const { return lhs.size() ; }
49 
50 private:
51  const VEC_TYPE& lhs ;
52  STORAGE rhs ;
54  Operator op ;
55 
56  inline int rhs_is_na(int i) const { return rhs ; }
57  inline int rhs_is_not_na(int i) const {
58  STORAGE x = lhs[i] ;
59  return Rcpp::traits::is_na<RTYPE>(x) ? x : op( x, rhs ) ;
60  }
61 
62 } ;
63 
64 
65 template <int RTYPE, typename Operator, typename T>
66 class Comparator_With_One_Value<RTYPE,Operator,false,T> :
67  public ::Rcpp::VectorBase< RTYPE, true, Comparator_With_One_Value<LGLSXP,Operator,false,T> > {
68 
69 public:
72  typedef int (Comparator_With_One_Value::*METHOD)(int) const ;
73 
74  Comparator_With_One_Value( const VEC_TYPE& lhs_, STORAGE rhs_ ) :
75  lhs(lhs_), rhs(rhs_), m(), op() {
76 
77  m = Rcpp::traits::is_na<RTYPE>(rhs) ?
80 
81  }
82 
83  inline int operator[]( R_xlen_t i ) const {
84  return (this->*m)(i) ;
85  }
86 
87  inline R_xlen_t size() const { return lhs.size() ; }
88 
89 private:
90  const VEC_TYPE& lhs ;
91  STORAGE rhs ;
93  Operator op ;
94 
95  inline int rhs_is_na(int i) const { return rhs ; }
96  inline int rhs_is_not_na(int i) const {
97  return op( lhs[i], rhs ) ;
98  }
99 
100 } ;
101 
102 
103 } // sugar
104 } // Rcpp
105 
106 
107 
108 
109 
110 #endif
int(Comparator_With_One_Value::* METHOD)(int) const
R_xlen_t size() const
Definition: VectorBase.h:49
traits::storage_type< RTYPE >::type STORAGE
Comparator_With_One_Value(const VEC_TYPE &lhs_, STORAGE rhs_)
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Rcpp API.
Definition: algo.h:28