Rcpp Version 1.0.14
Loading...
Searching...
No Matches
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
25namespace Rcpp{
26namespace sugar{
27
28template <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
32template <>
34 inline double operator()( double left, double right ) const {
35 return ( Rcpp::traits::is_na<REALSXP>( left ) || (left < right) ) ? left : right ;
36 }
37} ;
38template <> struct pmin_op<REALSXP,true,false> {
39 inline double operator()( double left, double right ) const {
40 return right < left ? right : left ;
41 }
42} ;
43template <> struct pmin_op<REALSXP,false,true> {
44 inline double operator()( double left, double right ) const {
45 return right < left ? right : left ;
46 }
47} ;
48template <> 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
56template <bool LHS_NA, bool RHS_NA>
58 inline int operator()(int left, int right) const {
59 return left < right ? left : right ;
60 }
61} ;
62
63
64// general case
65template <int RTYPE, bool NA> class pmin_op_Vector_Primitive {
66public:
68
70
71 inline STORAGE operator()( STORAGE left ) const {
72 return left < right ? left : right ;
73 }
74
75private:
77} ;
78// only special case we need to take care of
80public:
82
83 inline double operator()( double left ) const {
84 return ( Rcpp::traits::is_na<REALSXP>( left ) || (left < right) ) ? left : right ;
85 }
86
87private:
88 double right ;
89} ;
90
91
92
93
94template <
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> {
104public:
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
115private:
116 const LHS_T& lhs ;
117 const RHS_T& rhs ;
119} ;
120
121
122
123template <
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> {
132public:
135
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
141private:
142 const LHS_T& lhs ;
144} ;
145
146
147
148} // sugar
149
150template <
151 int RTYPE,
152 bool LHS_NA, typename LHS_T,
153 bool RHS_NA, typename RHS_T
154>
162
163template <
164 int RTYPE,
165 bool LHS_NA, typename LHS_T
166>
167inline sugar::Pmin_Vector_Primitive<RTYPE,LHS_NA,LHS_T>
174
175
176template <
177 int RTYPE,
178 bool RHS_NA, typename RHS_T
179>
180inline sugar::Pmin_Vector_Primitive<RTYPE,RHS_NA,RHS_T>
187
188
189} // Rcpp
190
191#endif
VECTOR & get_ref()
Definition VectorBase.h:37
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition pmin.h:133
STORAGE operator[](R_xlen_t i) const
Definition pmin.h:138
Pmin_Vector_Primitive(const LHS_T &lhs_, STORAGE rhs_)
Definition pmin.h:136
pmin_op_Vector_Primitive< RTYPE, LHS_NA > OPERATOR
Definition pmin.h:134
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition pmin.h:105
pmin_op< RTYPE, LHS_NA, RHS_NA > OPERATOR
Definition pmin.h:106
Pmin_Vector_Vector(const LHS_T &lhs_, const RHS_T &rhs_)
Definition pmin.h:108
R_xlen_t size() const
Definition pmin.h:113
STORAGE operator[](R_xlen_t i) const
Definition pmin.h:110
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition pmin.h:67
pmin_op_Vector_Primitive(STORAGE right_)
Definition pmin.h:69
STORAGE operator()(STORAGE left) const
Definition pmin.h:71
bool is_na< REALSXP >(double x)
Definition is_na.h:42
Rcpp API.
Definition algo.h:28
T as(SEXP x)
Definition as.h:151
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
int operator()(int left, int right) const
Definition pmin.h:58
double operator()(double left, double right) const
Definition pmin.h:49
double operator()(double left, double right) const
Definition pmin.h:44
double operator()(double left, double right) const
Definition pmin.h:39
double operator()(double left, double right) const
Definition pmin.h:34