Rcpp Version 1.0.14
Loading...
Searching...
No Matches
ifelse.h
Go to the documentation of this file.
1// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2//
3// ifelse.h: Rcpp R/C++ interface class library -- ifelse
4//
5// Copyright (C) 2010 - 2014 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__ifelse_h
23#define Rcpp__sugar__ifelse_h
24
25namespace Rcpp{
26namespace sugar{
27
28template <
29 int RTYPE,
30 bool COND_NA, typename COND_T,
31 bool LHS_NA , typename LHS_T,
32 bool RHS_NA , typename RHS_T
33 >
34class IfElse : public VectorBase<
35 RTYPE,
36 ( COND_NA || LHS_NA || RHS_NA ) ,
37 IfElse<RTYPE,COND_NA,COND_T,LHS_NA,LHS_T,RHS_NA,RHS_T>
38> {
39public:
44
45 // typedef typename Rcpp::traits::Extractor<RTYPE ,LHS_NA ,LHS_T>::type LHS_EXT ;
46 // typedef typename Rcpp::traits::Extractor<RTYPE ,RHS_NA ,RHS_T>::type RHS_EXT ;
47
48 IfElse( const COND_TYPE& cond_, const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
50 /* FIXME : cond, lhs and rhs must all have the same size */
51
53 }
54
55 inline STORAGE operator[]( R_xlen_t i ) const {
56 int x = cond[i] ;
57 if( Rcpp::traits::is_na<LGLSXP>(x) ) return Rcpp::traits::get_na<RTYPE>() ;
58 if( x ) return lhs[i] ;
59 return rhs[i] ;
60 }
61
62 inline R_xlen_t size() const { return cond.size() ; }
63
64private:
65 const COND_TYPE& cond ;
66 const LHS_T& lhs ;
67 const RHS_T& rhs ;
68
69} ;
70
71template <
72 int RTYPE,
73 typename COND_T,
74 bool LHS_NA , typename LHS_T,
75 bool RHS_NA , typename RHS_T
76 >
78 RTYPE,
79 ( LHS_NA || RHS_NA ) ,
80 IfElse<RTYPE,false,COND_T,LHS_NA,LHS_T,RHS_NA,RHS_T>
81> {
82public:
87
90
91 IfElse( const COND_TYPE& cond_, const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
93 /* FIXME : cond, lhs and rhs must all have the same size */
94 }
95
96 inline STORAGE operator[]( R_xlen_t i ) const {
97 if( cond[i] ) return lhs[i] ;
98 return rhs[i] ;
99 }
100
101 inline R_xlen_t size() const { return cond.size() ; }
102
103private:
104
106 const LHS_EXT& lhs ;
107 const RHS_EXT& rhs ;
108
109} ;
110
111
112/* ifelse( cond, primitive, Vector ) */
113
114template <
115 int RTYPE,
116 bool COND_NA, typename COND_T,
117 bool RHS_NA , typename RHS_T
118 >
120 RTYPE,
121 true ,
122 IfElse_Primitive_Vector<RTYPE,COND_NA,COND_T,RHS_NA,RHS_T>
123> {
124public:
128
130
132 cond(cond_), lhs(lhs_), rhs(rhs_.get_ref()) {
133 /* FIXME : cond, lhs and rhs must all have the sale size */
134 }
135
136 inline STORAGE operator[]( R_xlen_t i ) const {
137 int x = cond[i] ;
138 if( Rcpp::traits::is_na<LGLSXP>(x) ) return x ;
139 if( x ) return lhs ;
140 return rhs[i] ;
141 }
142
143 inline R_xlen_t size() const { return cond.size() ; }
144
145private:
148 const RHS_EXT& rhs ;
149
150} ;
151
152template <
153 int RTYPE,
154 typename COND_T,
155 bool RHS_NA , typename RHS_T
156 >
158 RTYPE,
159 true,
160 IfElse_Primitive_Vector<RTYPE,false,COND_T,RHS_NA,RHS_T>
161> {
162public:
167
169 cond(cond_), lhs(lhs_), rhs(rhs_.get_ref()) {
170 /* FIXME : cond, lhs and rhs must all have the same size */
171 }
172
173 inline STORAGE operator[]( R_xlen_t i ) const {
174 if( cond[i] ) return lhs ;
175 return rhs[i] ;
176 }
177
178 inline R_xlen_t size() const { return cond.size() ; }
179
180private:
183 const RHS_EXT& rhs ;
184
185} ;
186
187
188
189/* ifelse( cond, Vector, primitive ) */
190
191template <
192 int RTYPE,
193 bool COND_NA, typename COND_T,
194 bool LHS_NA , typename LHS_T
195 >
197 RTYPE,
198 true ,
199 IfElse_Vector_Primitive<RTYPE,COND_NA,COND_T,LHS_NA,LHS_T>
200> {
201public:
206
208 cond(cond_), lhs(lhs_.get_ref()), rhs(rhs_) {
209 /* FIXME : cond, lhs and rhs must all have the same size */
210 }
211
212 inline STORAGE operator[]( R_xlen_t i ) const {
213 int x = cond[i] ;
214 if( Rcpp::traits::is_na<LGLSXP>(x) ) return Rcpp::traits::get_na<RTYPE>() ;
215 if( x ) return lhs[i] ;
216 return rhs ;
217 }
218
219 inline R_xlen_t size() const { return cond.size() ; }
220
221private:
223 const LHS_EXT& lhs ;
224 const STORAGE rhs ;
225
226} ;
227
228template <
229 int RTYPE,
230 typename COND_T,
231 bool LHS_NA , typename LHS_T
232 >
234 RTYPE,
235 true ,
236 IfElse_Vector_Primitive<RTYPE,false,COND_T,LHS_NA,LHS_T>
237> {
238public:
243
245 cond(cond_), lhs(lhs_.get_ref()), rhs(rhs_) {
246 /* FIXME : cond, lhs and rhs must all have the sale size */
247 }
248
249 inline STORAGE operator[]( R_xlen_t i ) const {
250 if( cond[i] ) return lhs[i] ;
251 return rhs ;
252 }
253
254 inline R_xlen_t size() const { return cond.size() ; }
255
256private:
258 const LHS_EXT& lhs ;
259 const STORAGE rhs ;
260
261} ;
262
263
264
265
266
267/* ifelse( cond, primitive, primitive ) */
268
269template <
270 int RTYPE,
271 bool COND_NA, typename COND_T
272 >
274 RTYPE,
275 true ,
276 IfElse_Primitive_Primitive<RTYPE,COND_NA,COND_T>
277> {
278public:
281
283 cond(cond_), lhs(lhs_), rhs(rhs_) {
284 /* FIXME : cond, lhs and rhs must all have the same size */
285 }
286
287 inline STORAGE operator[]( R_xlen_t i ) const {
288 int x = cond[i] ;
289 if( Rcpp::traits::is_na<LGLSXP>(x) ) return Rcpp::traits::get_na<RTYPE>() ;
290 return x ? lhs : rhs ;
291 }
292
293 inline R_xlen_t size() const { return cond.size() ; }
294
295private:
300
301} ;
302
303template <
304 int RTYPE, typename COND_T
305 >
307 RTYPE,
308 true ,
309 IfElse_Primitive_Primitive<RTYPE,false,COND_T>
310> {
311public:
314
316 cond(cond_), lhs(lhs_), rhs(rhs_) {
317 /* FIXME : cond, lhs and rhs must all have the same size */
318 }
319
320 inline STORAGE operator[]( R_xlen_t i ) const {
321 return cond[i] ? lhs : rhs ;
322 }
323
324 inline R_xlen_t size() const { return cond.size() ; }
325
326private:
330
331} ;
332
333} // sugar
334
335template <
336 int RTYPE,
337 bool COND_NA, typename COND_T,
338 bool LHS_NA , typename LHS_T,
339 bool RHS_NA , typename RHS_T
340 >
349
350
351template <
352 int RTYPE,
353 bool COND_NA, typename COND_T,
354 bool RHS_NA , typename RHS_T
355 >
356inline sugar::IfElse_Primitive_Vector< RTYPE,COND_NA,COND_T,RHS_NA,RHS_T >
364
365template <
366 int RTYPE,
367 bool COND_NA, typename COND_T,
368 bool RHS_NA , typename RHS_T
369 >
370inline sugar::IfElse_Vector_Primitive< RTYPE,COND_NA,COND_T,RHS_NA,RHS_T >
378
379template<
380 bool COND_NA, typename COND_T
381>
382inline sugar::IfElse_Primitive_Primitive< REALSXP,COND_NA,COND_T >
385 double lhs,
386 double rhs
387 ){
389}
390
391template<
392 bool COND_NA, typename COND_T
393>
394inline sugar::IfElse_Primitive_Primitive< INTSXP,COND_NA,COND_T >
397 int lhs,
398 int rhs
399 ){
401}
402
403template<
404 bool COND_NA, typename COND_T
405>
406inline sugar::IfElse_Primitive_Primitive< CPLXSXP,COND_NA,COND_T >
409 Rcomplex lhs,
410 Rcomplex rhs
411 ){
413}
414
415template<
416 bool COND_NA, typename COND_T
417>
418inline sugar::IfElse_Primitive_Primitive< LGLSXP,COND_NA,COND_T >
421 bool lhs,
422 bool rhs
423 ){
425}
426
427
428} // Rcpp
429
430#endif
R_xlen_t size() const
Definition VectorBase.h:49
IfElse(const COND_TYPE &cond_, const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition ifelse.h:91
Rcpp::traits::Extractor< RTYPE, LHS_NA, LHS_T >::type LHS_EXT
Definition ifelse.h:88
Rcpp::traits::Extractor< RTYPE, RHS_NA, RHS_T >::type RHS_EXT
Definition ifelse.h:89
IfElse_Primitive_Primitive(const COND_TYPE &cond_, STORAGE lhs_, STORAGE rhs_)
Definition ifelse.h:315
Rcpp::VectorBase< LGLSXP, false, COND_T > COND_TYPE
Definition ifelse.h:312
traits::storage_type< RTYPE >::type STORAGE
Definition ifelse.h:280
STORAGE operator[](R_xlen_t i) const
Definition ifelse.h:287
Rcpp::VectorBase< LGLSXP, COND_NA, COND_T > COND_TYPE
Definition ifelse.h:279
IfElse_Primitive_Primitive(const COND_TYPE &cond_, STORAGE lhs_, STORAGE rhs_)
Definition ifelse.h:282
IfElse_Primitive_Vector(const COND_TYPE &cond_, STORAGE lhs_, const RHS_TYPE &rhs_)
Definition ifelse.h:168
Rcpp::traits::Extractor< RTYPE, RHS_NA, RHS_T >::type RHS_EXT
Definition ifelse.h:166
Rcpp::VectorBase< RTYPE,RHS_NA,RHS_T > RHS_TYPE
Definition ifelse.h:126
Rcpp::traits::Extractor< RTYPE, RHS_NA, RHS_T >::type RHS_EXT
Definition ifelse.h:129
STORAGE operator[](R_xlen_t i) const
Definition ifelse.h:136
traits::storage_type< RTYPE >::type STORAGE
Definition ifelse.h:127
IfElse_Primitive_Vector(const COND_TYPE &cond_, STORAGE lhs_, const RHS_TYPE &rhs_)
Definition ifelse.h:131
Rcpp::VectorBase< LGLSXP, COND_NA, COND_T > COND_TYPE
Definition ifelse.h:125
Rcpp::traits::Extractor< RTYPE, LHS_NA, LHS_T >::type LHS_EXT
Definition ifelse.h:242
IfElse_Vector_Primitive(const COND_TYPE &cond_, const LHS_TYPE &lhs_, STORAGE rhs_)
Definition ifelse.h:244
Rcpp::VectorBase< RTYPE,LHS_NA,LHS_T > LHS_TYPE
Definition ifelse.h:203
Rcpp::traits::Extractor< RTYPE, LHS_NA, LHS_T >::type LHS_EXT
Definition ifelse.h:205
IfElse_Vector_Primitive(const COND_TYPE &cond_, const LHS_TYPE &lhs_, STORAGE rhs_)
Definition ifelse.h:207
Rcpp::VectorBase< LGLSXP, COND_NA, COND_T > COND_TYPE
Definition ifelse.h:202
traits::storage_type< RTYPE >::type STORAGE
Definition ifelse.h:204
STORAGE operator[](R_xlen_t i) const
Definition ifelse.h:212
Rcpp::VectorBase< RTYPE,LHS_NA,LHS_T > LHS_TYPE
Definition ifelse.h:41
Rcpp::VectorBase< RTYPE,RHS_NA,RHS_T > RHS_TYPE
Definition ifelse.h:42
const RHS_T & rhs
Definition ifelse.h:67
const LHS_T & lhs
Definition ifelse.h:66
const COND_TYPE & cond
Definition ifelse.h:65
STORAGE operator[](R_xlen_t i) const
Definition ifelse.h:55
R_xlen_t size() const
Definition ifelse.h:62
IfElse(const COND_TYPE &cond_, const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition ifelse.h:48
traits::storage_type< RTYPE >::type STORAGE
Definition ifelse.h:43
Rcpp::VectorBase< LGLSXP, COND_NA, COND_T > COND_TYPE
Definition ifelse.h:40
#define RCPP_DEBUG(MSG)
Definition debug.h:43
#define DEMANGLE(__TYPE__)
Definition exceptions.h:382
bool is_na< LGLSXP >(int x)
Definition is_na.h:57
Rcpp API.
Definition algo.h:28
sugar::IfElse< RTYPE, COND_NA, COND_T, LHS_NA, LHS_T, RHS_NA, RHS_T > ifelse(const Rcpp::VectorBase< LGLSXP, COND_NA, COND_T > &cond, const Rcpp::VectorBase< RTYPE, LHS_NA, LHS_T > &lhs, const Rcpp::VectorBase< RTYPE, RHS_NA, RHS_T > &rhs)
Definition ifelse.h:342
T as(SEXP x)
Definition as.h:151