Rcpp Version 0.12.12
pmin.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // pmin.h: Rcpp R/C++ interface class library -- pmin
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__pmin_h
23 #define Rcpp__sugar__pmin_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <int RTYPE, bool LHS_NA, bool RHS_NA> struct pmin_op ;
29 
30 // specializations for double.
31 // we use the fact that NA < x is false
32 template <>
33 struct pmin_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 pmin_op<REALSXP,true,false> {
39  inline double operator()( double left, double right ) const {
40  return right < left ? right : left ;
41  }
42 } ;
43 template <> struct pmin_op<REALSXP,false,true> {
44  inline double operator()( double left, double right ) const {
45  return right < left ? right : left ;
46  }
47 } ;
48 template <> struct pmin_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 pmin_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 pmin_op_Vector_Primitive {
66 public:
68 
69  pmin_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:
76  STORAGE right ;
77 } ;
78 // only special case we need to take care of
79 template <> class pmin_op_Vector_Primitive<REALSXP,true> {
80 public:
81  pmin_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  Pmin_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>
103 > {
104 public:
107 
108  Pmin_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 ;
118  OPERATOR op ;
119 } ;
120 
121 
122 
123 template <
124  int RTYPE,
125  bool LHS_NA, typename LHS_T
126  >
128  RTYPE ,
129  true ,
130  Pmin_Vector_Primitive<RTYPE,LHS_NA,LHS_T>
131 > {
132 public:
135 
136  Pmin_Vector_Primitive( const LHS_T& lhs_, STORAGE rhs_ ) : lhs(lhs_), op(rhs_) {}
137 
138  inline STORAGE operator[]( R_xlen_t i ) const { return op( lhs[i] ) ; }
139  inline R_xlen_t size() const { return lhs.size() ; }
140 
141 private:
142  const LHS_T& lhs ;
143  OPERATOR op ;
144 } ;
145 
146 
147 
148 } // sugar
149 
150 template <
151  int RTYPE,
152  bool LHS_NA, typename LHS_T,
153  bool RHS_NA, typename RHS_T
154 >
159  ){
161 }
162 
163 template <
164  int RTYPE,
165  bool LHS_NA, typename LHS_T
166 >
171  ){
173 }
174 
175 
176 template <
177  int RTYPE,
178  bool RHS_NA, typename RHS_T
179 >
184  ){
186 }
187 
188 
189 } // Rcpp
190 
191 #endif
double operator()(double left, double right) const
Definition: pmin.h:49
STORAGE operator[](R_xlen_t i) const
Definition: pmin.h:110
VECTOR & get_ref()
Definition: VectorBase.h:37
R_xlen_t size() const
Definition: pmin.h:139
R_xlen_t size() const
Definition: pmin.h:113
int operator()(int left, int right) const
Definition: pmin.h:58
double operator()(double left, double right) const
Definition: pmin.h:34
bool is_na< REALSXP >(double x)
Definition: is_na.h:41
Pmin_Vector_Vector(const LHS_T &lhs_, const RHS_T &rhs_)
Definition: pmin.h:108
sugar::Pmin_Vector_Vector< RTYPE, LHS_NA, LHS_T, RHS_NA, RHS_T > pmin(const Rcpp::VectorBase< RTYPE, LHS_NA, LHS_T > &lhs, const Rcpp::VectorBase< RTYPE, RHS_NA, RHS_T > &rhs)
Definition: pmin.h:156
double operator()(double left, double right) const
Definition: pmin.h:44
double operator()(double left, double right) const
Definition: pmin.h:39
pmin_op_Vector_Primitive< RTYPE, LHS_NA > OPERATOR
Definition: pmin.h:134
pmin_op_Vector_Primitive(STORAGE right_)
Definition: pmin.h:69
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: pmin.h:105
STORAGE operator[](R_xlen_t i) const
Definition: pmin.h:138
Pmin_Vector_Primitive(const LHS_T &lhs_, STORAGE rhs_)
Definition: pmin.h:136
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: pmin.h:67
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: pmin.h:133
Rcpp API.
Definition: algo.h:28
pmin_op< RTYPE, LHS_NA, RHS_NA > OPERATOR
Definition: pmin.h:106
STORAGE operator()(STORAGE left) const
Definition: pmin.h:71