Rcpp Version 1.0.9
pmax.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // pmax.h: Rcpp R/C++ interface class library -- pmax
4 //
5 // Copyright (C) 2010 - 2012 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__pmax_h
23 #define Rcpp__sugar__pmax_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <int RTYPE, bool LHS_NA, bool RHS_NA> struct pmax_op ;
29 
30 // specializations for double.
31 // we use the fact that NA < x is false
32 template <>
33 struct pmax_op<REALSXP,true,true>{
34  inline double operator()( double left, double right ) const {
35  return ( Rcpp::traits::is_na<REALSXP>( left ) || (left > right) ) ? left : right ;
36  }
37 } ;
38 template <> struct pmax_op<REALSXP,true,false> {
39  inline double operator()( double left, double right ) const {
40  return right > left ? right : left ;
41  }
42 } ;
43 template <> struct pmax_op<REALSXP,false,true> {
44  inline double operator()( double left, double right ) const {
45  return right > left ? right : left ;
46  }
47 } ;
48 template <> struct pmax_op<REALSXP,false,false> {
49  inline double operator()( double left, double right ) const {
50  return left > right ? left : right ;
51  }
52 } ;
53 
54 // specializations for INTSXP. Since NA is represented as the smallest
55 // int, NA is always the smallest, so it is safe to return NA
56 template <bool LHS_NA, bool RHS_NA>
57 struct pmax_op<INTSXP,LHS_NA,RHS_NA> {
58  inline int operator()(int left, int right) const {
59  return left > right ? left : right ;
60  }
61 } ;
62 
63 
64 // general case
65 template <int RTYPE, bool NA> class pmax_op_Vector_Primitive {
66 public:
68 
69  pmax_op_Vector_Primitive( STORAGE right_ ) : right(right_) {}
70 
71  inline STORAGE operator()( STORAGE left ) const {
72  return left > right ? left : right ;
73  }
74 
75 private:
77 } ;
78 // only special case we need to take care of
79 template <> class pmax_op_Vector_Primitive<REALSXP,true> {
80 public:
81  pmax_op_Vector_Primitive( double right_ ) : right(right_) {}
82 
83  inline double operator()( double left ) const {
84  return ( Rcpp::traits::is_na<REALSXP>( left ) || (left > right) ) ? left : right ;
85  }
86 
87 private:
88  double right ;
89 } ;
90 
91 
92 
93 
94 template <
95  int RTYPE,
96  bool LHS_NA, typename LHS_T,
97  bool RHS_NA, typename RHS_T
98  >
100  RTYPE ,
101  ( LHS_NA || RHS_NA ) ,
102  Pmax_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>
103 > {
104 public:
107 
108  Pmax_Vector_Vector( const LHS_T& lhs_, const RHS_T& rhs_ ) : lhs(lhs_), rhs(rhs_), op() {}
109 
110  inline STORAGE operator[]( R_xlen_t i ) const {
111  return op( lhs[i], rhs[i] ) ;
112  }
113  inline R_xlen_t size() const { return lhs.size() ; }
114 
115 private:
116  const LHS_T& lhs ;
117  const RHS_T& rhs ;
119 } ;
120 
121 
122 
123 template <
124  int RTYPE,
125  bool LHS_NA, typename LHS_T
126  >
128  RTYPE ,
129  true ,
130  Pmax_Vector_Primitive<RTYPE,LHS_NA,LHS_T>
131 > {
132 public:
135 
136  Pmax_Vector_Primitive( const LHS_T& lhs_, STORAGE rhs_ ) : lhs(lhs_), op(rhs_) {}
137 
138  inline STORAGE operator[]( R_xlen_t i ) const {
139  return op( lhs[i] ) ;
140  }
141  inline R_xlen_t size() const { return lhs.size() ; }
142 
143 private:
144  const LHS_T& lhs ;
146 } ;
147 
148 
149 
150 } // sugar
151 
152 template <
153  int RTYPE,
154  bool LHS_NA, typename LHS_T,
155  bool RHS_NA, typename RHS_T
156 >
161  ){
163 }
164 
165 template <
166  int RTYPE,
167  bool LHS_NA, typename LHS_T
168 >
169 inline sugar::Pmax_Vector_Primitive<RTYPE,LHS_NA,LHS_T>
173  ){
175 }
176 
177 
178 template <
179  int RTYPE,
180  bool RHS_NA, typename RHS_T
181 >
182 inline sugar::Pmax_Vector_Primitive<RTYPE,RHS_NA,RHS_T>
186  ){
188 }
189 
190 
191 } // Rcpp
192 
193 #endif
VECTOR & get_ref()
Definition: VectorBase.h:37
STORAGE operator[](R_xlen_t i) const
Definition: pmax.h:138
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: pmax.h:133
pmax_op_Vector_Primitive< RTYPE, LHS_NA > OPERATOR
Definition: pmax.h:134
Pmax_Vector_Primitive(const LHS_T &lhs_, STORAGE rhs_)
Definition: pmax.h:136
pmax_op< RTYPE, LHS_NA, RHS_NA > OPERATOR
Definition: pmax.h:106
STORAGE operator[](R_xlen_t i) const
Definition: pmax.h:110
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: pmax.h:105
Pmax_Vector_Vector(const LHS_T &lhs_, const RHS_T &rhs_)
Definition: pmax.h:108
R_xlen_t size() const
Definition: pmax.h:113
pmax_op_Vector_Primitive(STORAGE right_)
Definition: pmax.h:69
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: pmax.h:67
STORAGE operator()(STORAGE left) const
Definition: pmax.h:71
bool is_na< REALSXP >(double x)
Definition: is_na.h:42
Rcpp API.
Definition: algo.h:28
sugar::Pmax_Vector_Vector< RTYPE, LHS_NA, LHS_T, RHS_NA, RHS_T > pmax(const Rcpp::VectorBase< RTYPE, LHS_NA, LHS_T > &lhs, const Rcpp::VectorBase< RTYPE, RHS_NA, RHS_T > &rhs)
Definition: pmax.h:158
int operator()(int left, int right) const
Definition: pmax.h:58
double operator()(double left, double right) const
Definition: pmax.h:49
double operator()(double left, double right) const
Definition: pmax.h:44
double operator()(double left, double right) const
Definition: pmax.h:39
double operator()(double left, double right) const
Definition: pmax.h:34