Rcpp Version 0.12.12
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 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <
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  >
34 class 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 > {
39 public:
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_ ) :
49  cond(cond_), lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {
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 
64 private:
65  const COND_TYPE& cond ;
66  const LHS_T& lhs ;
67  const RHS_T& rhs ;
68 
69 } ;
70 
71 template <
72  int RTYPE,
73  typename COND_T,
74  bool LHS_NA , typename LHS_T,
75  bool RHS_NA , typename RHS_T
76  >
77 class IfElse<RTYPE,false,COND_T,LHS_NA,LHS_T,RHS_NA,RHS_T> : public VectorBase<
78  RTYPE,
79  ( LHS_NA || RHS_NA ) ,
80  IfElse<RTYPE,false,COND_T,LHS_NA,LHS_T,RHS_NA,RHS_T>
81 > {
82 public:
87 
90 
91  IfElse( const COND_TYPE& cond_, const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
92  cond(cond_), lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {
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 
103 private:
104 
105  const COND_TYPE& cond ;
106  const LHS_EXT& lhs ;
107  const RHS_EXT& rhs ;
108 
109 } ;
110 
111 
112 /* ifelse( cond, primitive, Vector ) */
113 
114 template <
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 > {
124 public:
128 
130 
131  IfElse_Primitive_Vector( const COND_TYPE& cond_, STORAGE lhs_, const RHS_TYPE& rhs_ ) :
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 
145 private:
146  const COND_TYPE& cond ;
147  STORAGE lhs ;
148  const RHS_EXT& rhs ;
149 
150 } ;
151 
152 template <
153  int RTYPE,
154  typename COND_T,
155  bool RHS_NA , typename RHS_T
156  >
157 class IfElse_Primitive_Vector<RTYPE,false,COND_T,RHS_NA,RHS_T> : public VectorBase<
158  RTYPE,
159  true,
160  IfElse_Primitive_Vector<RTYPE,false,COND_T,RHS_NA,RHS_T>
161 > {
162 public:
167 
168  IfElse_Primitive_Vector( const COND_TYPE& cond_, STORAGE lhs_, const RHS_TYPE& rhs_ ) :
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 
180 private:
181  const COND_TYPE& cond ;
182  STORAGE lhs ;
183  const RHS_EXT& rhs ;
184 
185 } ;
186 
187 
188 
189 /* ifelse( cond, Vector, primitive ) */
190 
191 template <
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 > {
201 public:
206 
207  IfElse_Vector_Primitive( const COND_TYPE& cond_, const LHS_TYPE& lhs_, STORAGE rhs_ ) :
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 
221 private:
222  const COND_TYPE& cond ;
223  const LHS_EXT& lhs ;
224  const STORAGE rhs ;
225 
226 } ;
227 
228 template <
229  int RTYPE,
230  typename COND_T,
231  bool LHS_NA , typename LHS_T
232  >
233 class IfElse_Vector_Primitive<RTYPE,false,COND_T,LHS_NA,LHS_T> : public VectorBase<
234  RTYPE,
235  true ,
236  IfElse_Vector_Primitive<RTYPE,false,COND_T,LHS_NA,LHS_T>
237 > {
238 public:
243 
244  IfElse_Vector_Primitive( const COND_TYPE& cond_, const LHS_TYPE& lhs_, STORAGE rhs_ ) :
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 
256 private:
257  const COND_TYPE& cond ;
258  const LHS_EXT& lhs ;
259  const STORAGE rhs ;
260 
261 } ;
262 
263 
264 
265 
266 
267 /* ifelse( cond, primitive, primitive ) */
268 
269 template <
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 > {
278 public:
281 
282  IfElse_Primitive_Primitive( const COND_TYPE& cond_, STORAGE lhs_, STORAGE rhs_ ) :
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 
295 private:
296  const COND_TYPE& cond ;
297  STORAGE lhs ;
298  STORAGE rhs ;
299  STORAGE na ;
300 
301 } ;
302 
303 template <
304  int RTYPE, typename COND_T
305  >
306 class IfElse_Primitive_Primitive<RTYPE,false,COND_T> : public VectorBase<
307  RTYPE,
308  true ,
309  IfElse_Primitive_Primitive<RTYPE,false,COND_T>
310 > {
311 public:
314 
315  IfElse_Primitive_Primitive( const COND_TYPE& cond_, STORAGE lhs_, STORAGE rhs_ ) :
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 
326 private:
327  const COND_TYPE& cond ;
328  STORAGE lhs ;
329  STORAGE rhs ;
330 
331 } ;
332 
333 } // sugar
334 
335 template <
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  >
346  ){
348 }
349 
350 
351 template <
352  int RTYPE,
353  bool COND_NA, typename COND_T,
354  bool RHS_NA , typename RHS_T
355  >
361  ){
363 }
364 
365 template <
366  int RTYPE,
367  bool COND_NA, typename COND_T,
368  bool RHS_NA , typename RHS_T
369  >
375  ){
377 }
378 
379 template<
380  bool COND_NA, typename COND_T
381 >
385  double lhs,
386  double rhs
387  ){
389 }
390 
391 template<
392  bool COND_NA, typename COND_T
393 >
397  int lhs,
398  int rhs
399  ){
401 }
402 
403 template<
404  bool COND_NA, typename COND_T
405 >
409  Rcomplex lhs,
410  Rcomplex rhs
411  ){
413 }
414 
415 template<
416  bool COND_NA, typename COND_T
417 >
421  bool lhs,
422  bool rhs
423  ){
425 }
426 
427 
428 } // Rcpp
429 
430 #endif
IfElse_Primitive_Primitive(const COND_TYPE &cond_, STORAGE lhs_, STORAGE rhs_)
Definition: ifelse.h:315
Rcpp::VectorBase< LGLSXP, COND_NA, COND_T > COND_TYPE
Definition: ifelse.h:40
R_xlen_t size() const
Definition: VectorBase.h:49
#define RCPP_DEBUG(MSG)
Definition: debug.h:43
STORAGE operator[](R_xlen_t i) const
Definition: ifelse.h:287
Rcpp::traits::Extractor< RTYPE,RHS_NA,RHS_T >::type RHS_EXT
Definition: ifelse.h:129
IfElse(const COND_TYPE &cond_, const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: ifelse.h:91
Rcpp::traits::Extractor< RTYPE,RHS_NA,RHS_T >::type RHS_EXT
Definition: ifelse.h:89
#define DEMANGLE(__TYPE__)
Definition: exceptions.h:315
IfElse_Primitive_Primitive(const COND_TYPE &cond_, STORAGE lhs_, STORAGE rhs_)
Definition: ifelse.h:282
IfElse_Vector_Primitive(const COND_TYPE &cond_, const LHS_TYPE &lhs_, STORAGE rhs_)
Definition: ifelse.h:207
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:212
Rcpp::traits::Extractor< RTYPE,RHS_NA,RHS_T >::type RHS_EXT
Definition: ifelse.h:166
traits::storage_type< RTYPE >::type STORAGE
Definition: ifelse.h:43
Rcpp::VectorBase< LGLSXP, false, COND_T > COND_TYPE
Definition: ifelse.h:312
IfElse_Primitive_Vector(const COND_TYPE &cond_, STORAGE lhs_, const RHS_TYPE &rhs_)
Definition: ifelse.h:168
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
Rcpp::VectorBase< RTYPE,LHS_NA,LHS_T > LHS_TYPE
Definition: ifelse.h:203
bool is_na< LGLSXP >(int x)
Definition: is_na.h:57
Rcpp::VectorBase< RTYPE,LHS_NA,LHS_T > LHS_TYPE
Definition: ifelse.h:41
const RHS_T & rhs
Definition: ifelse.h:67
R_xlen_t size() const
Definition: ifelse.h:62
Rcpp::VectorBase< RTYPE,RHS_NA,RHS_T > RHS_TYPE
Definition: ifelse.h:126
Rcpp::traits::Extractor< RTYPE,LHS_NA,LHS_T >::type LHS_EXT
Definition: ifelse.h:205
STORAGE operator[](R_xlen_t i) const
Definition: ifelse.h:55
Rcpp::traits::Extractor< RTYPE,LHS_NA,LHS_T >::type LHS_EXT
Definition: ifelse.h:242
STORAGE operator[](R_xlen_t i) const
Definition: ifelse.h:136
traits::storage_type< RTYPE >::type STORAGE
Definition: ifelse.h:280
Rcpp::VectorBase< LGLSXP, COND_NA, COND_T > COND_TYPE
Definition: ifelse.h:125
traits::storage_type< RTYPE >::type STORAGE
Definition: ifelse.h:127
Rcpp::VectorBase< RTYPE,RHS_NA,RHS_T > RHS_TYPE
Definition: ifelse.h:42
Rcpp::VectorBase< LGLSXP, COND_NA, COND_T > COND_TYPE
Definition: ifelse.h:279
Rcpp::traits::Extractor< RTYPE,LHS_NA,LHS_T >::type LHS_EXT
Definition: ifelse.h:88
Rcpp API.
Definition: algo.h:28
Rcpp::VectorBase< LGLSXP, COND_NA, COND_T > COND_TYPE
Definition: ifelse.h:202
IfElse_Primitive_Vector(const COND_TYPE &cond_, STORAGE lhs_, const RHS_TYPE &rhs_)
Definition: ifelse.h:131
IfElse_Vector_Primitive(const COND_TYPE &cond_, const LHS_TYPE &lhs_, STORAGE rhs_)
Definition: ifelse.h:244
traits::storage_type< RTYPE >::type STORAGE
Definition: ifelse.h:204
IfElse(const COND_TYPE &cond_, const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: ifelse.h:48