Rcpp Version 1.0.9
or.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // or.h: Rcpp R/C++ interface class library --
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__logical_or_h
23 #define Rcpp__sugar__logical_or_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
30 public SingleLogicalResult<
31  (LHS_NA || RHS_NA) ,
32  Or_SingleLogicalResult_SingleLogicalResult<LHS_NA,LHS_T,RHS_NA,RHS_T>
33  >
34 {
35 public:
38  typedef SingleLogicalResult<
39  (LHS_NA || RHS_NA) ,
41  > BASE ;
42 
44  lhs(lhs_), rhs(rhs_){} ;
45 
46  inline void apply(){
47  int left = lhs.get() ;
48  if( Rcpp::traits::is_na<LGLSXP>( left ) ){
49  BASE::set( left ) ;
50  } else if( left == TRUE ){
51  BASE::set( TRUE ) ;
52  } else {
53  BASE::set( rhs.get() ) ;
54  }
55  }
56 
57 private:
58  const LHS_TYPE& lhs ;
59  const RHS_TYPE& rhs ;
60 
61 } ;
62 
63 // special version when we know the rhs is not NA
64 template <bool LHS_NA, typename LHS_T, typename RHS_T>
65 class Or_SingleLogicalResult_SingleLogicalResult<LHS_NA,LHS_T,false,RHS_T> :
66 public SingleLogicalResult<
67  LHS_NA ,
68  Or_SingleLogicalResult_SingleLogicalResult<LHS_NA,LHS_T,false,RHS_T>
69  >
70 {
71 public:
74  typedef SingleLogicalResult<
75  LHS_NA,
77  > BASE ;
78 
80  lhs(lhs_), rhs(rhs_){} ;
81 
82  inline void apply(){
83  // here we know rhs does not have NA, so we start with the rhs
84  int right = rhs.get() ;
85  if( right == TRUE ){
86  BASE::set( TRUE ) ;
87  } else {
88  BASE::set( lhs.get() ) ;
89  }
90  }
91 
92 private:
93  const LHS_TYPE& lhs ;
94  const RHS_TYPE& rhs ;
95 
96 } ;
97 
98 
99 // special version when we know the lhs is not NA
100 template <typename LHS_T, bool RHS_NA, typename RHS_T>
101 class Or_SingleLogicalResult_SingleLogicalResult<false,LHS_T,RHS_NA,RHS_T> :
102 public SingleLogicalResult<
103  RHS_NA ,
104  Or_SingleLogicalResult_SingleLogicalResult<false,LHS_T,RHS_NA,RHS_T>
105  >
106 {
107 public:
110  typedef SingleLogicalResult<
111  RHS_NA,
113  > BASE ;
114 
116  lhs(lhs_), rhs(rhs_){} ;
117 
118  inline void apply(){
119  // here we know lhs does not have NA, so we start with the rhs
120  int left = lhs.get() ;
121  if( left == TRUE ){
122  BASE::set( TRUE ) ;
123  } else {
124  BASE::set( rhs.get() ) ;
125  }
126  }
127 
128 private:
129  const LHS_TYPE& lhs ;
130  const RHS_TYPE& rhs ;
131 
132 } ;
133 
134 // special version when we know both the lhs and the rhs are not NA
135 template <typename LHS_T, typename RHS_T>
136 class Or_SingleLogicalResult_SingleLogicalResult<false,LHS_T,false,RHS_T> :
137 public SingleLogicalResult<
138  false ,
139  Or_SingleLogicalResult_SingleLogicalResult<false,LHS_T,false,RHS_T>
140  >
141 {
142 public:
145  typedef SingleLogicalResult<
146  false,
148  > BASE ;
149 
151  lhs(lhs_), rhs(rhs_){} ;
152 
153  inline void apply(){
154  int left = lhs.get() ;
155  if( left == TRUE ){
156  BASE::set( TRUE ) ;
157  } else {
158  BASE::set( rhs.get() ) ;
159  }
160  }
161 
162 private:
163  const LHS_TYPE& lhs ;
164  const RHS_TYPE& rhs ;
165 
166 } ;
167 
168 
169 template <bool LHS_NA, typename LHS_T>
171 public SingleLogicalResult<
172  LHS_NA ,
173  And_SingleLogicalResult_bool<LHS_NA,LHS_T>
174  >
175 {
176 public:
178  typedef SingleLogicalResult<
179  LHS_NA ,
181  > BASE ;
182 
183  Or_SingleLogicalResult_bool( const LHS_TYPE& lhs_, bool rhs_) :
184  lhs(lhs_), rhs(rhs_){} ;
185 
186  inline void apply(){
187  if( rhs ){
188  BASE::set( TRUE ) ;
189  } else{
190  BASE::set( lhs.get() ) ;
191  }
192  }
193 
194 private:
195  const LHS_TYPE& lhs ;
196  bool rhs ;
197 
198 } ;
199 
200 // (LogicalExpression) | (LogicalExpression)
201 template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
202 class Or_LogicalExpression_LogicalExpression : public Rcpp::VectorBase< LGLSXP, true, Or_LogicalExpression_LogicalExpression<LHS_NA,LHS_T,RHS_NA,RHS_T> >{
203 public:
206 
207  Or_LogicalExpression_LogicalExpression( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : lhs(lhs_), rhs(rhs_){}
208 
209  inline int operator[]( R_xlen_t i ) const{
210  if( lhs[i] == TRUE || rhs[i] == TRUE ) return TRUE ;
211  if( lhs[i] == FALSE && rhs[i] == FALSE ) return FALSE ;
212  return NA_LOGICAL;
213  }
214  inline R_xlen_t size() const { return lhs.size(); }
215 
216 private:
217  const LHS_TYPE& lhs ;
218  const RHS_TYPE& rhs ;
219 } ;
220 template <typename LHS_T, bool RHS_NA, typename RHS_T>
221 class Or_LogicalExpression_LogicalExpression<false,LHS_T,RHS_NA,RHS_T>
222  : public Rcpp::VectorBase< LGLSXP, true, Or_LogicalExpression_LogicalExpression<false,LHS_T,RHS_NA,RHS_T> >{
223 public:
226 
227  Or_LogicalExpression_LogicalExpression( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : lhs(lhs_), rhs(rhs_){}
228 
229  inline int operator[]( R_xlen_t i ) const{
230  if( lhs[i] == TRUE || rhs[i] == TRUE ) return TRUE ;
231  if( rhs[i] == NA_LOGICAL ) return NA_LOGICAL ;
232  return FALSE ;
233  }
234  inline R_xlen_t size() const { return lhs.size(); }
235 
236 private:
237  const LHS_TYPE& lhs ;
238  const RHS_TYPE& rhs ;
239 } ;
240 template <bool LHS_NA, typename LHS_T, typename RHS_T>
241 class Or_LogicalExpression_LogicalExpression<LHS_NA,LHS_T,false,RHS_T>
242  : public Rcpp::VectorBase< LGLSXP, true, Or_LogicalExpression_LogicalExpression<LHS_NA,LHS_T,false,RHS_T> >{
243 public:
246 
247  Or_LogicalExpression_LogicalExpression( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : lhs(lhs_), rhs(rhs_){}
248 
249  inline int operator[]( R_xlen_t i ) const{
250  if( lhs[i] == TRUE || rhs[i] == TRUE ) return TRUE ;
251  if( lhs[i] == NA_LOGICAL ) return NA_LOGICAL ;
252  return FALSE;
253  }
254  inline R_xlen_t size() const { return lhs.size(); }
255 
256 private:
257  const LHS_TYPE& lhs ;
258  const RHS_TYPE& rhs ;
259 } ;
260 template <typename LHS_T, typename RHS_T>
261 class Or_LogicalExpression_LogicalExpression<false,LHS_T,false,RHS_T>
262  : public Rcpp::VectorBase< LGLSXP, false, Or_LogicalExpression_LogicalExpression<false,LHS_T,false,RHS_T> >{
263 public:
266 
267  Or_LogicalExpression_LogicalExpression( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : lhs(lhs_), rhs(rhs_){}
268 
269  inline int operator[]( R_xlen_t i ) const{
270  if( lhs[i] == TRUE || rhs[i] == TRUE ) return TRUE ;
271  return FALSE;
272  }
273  inline R_xlen_t size() const { return lhs.size(); }
274 
275 private:
276  const LHS_TYPE& lhs ;
277  const RHS_TYPE& rhs ;
278 } ;
279 
280 
281 
282 } // sugar
283 } // Rcpp
284 
285 template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
290 ){
292 }
293 
294 template <bool LHS_NA, typename LHS_T>
298  bool rhs
299 ){
301 }
302 
303 template <bool LHS_NA, typename LHS_T>
306  bool rhs,
308 ){
310 }
311 
312 // (logical expression) | (logical expression)
313 template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
318 ){
320 }
321 
322 
323 #endif
R_xlen_t size() const
Definition: VectorBase.h:49
Or_LogicalExpression_LogicalExpression(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: or.h:247
Or_LogicalExpression_LogicalExpression(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: or.h:227
Or_LogicalExpression_LogicalExpression(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: or.h:267
Rcpp::VectorBase< LGLSXP, RHS_NA, RHS_T > RHS_TYPE
Definition: or.h:205
Or_LogicalExpression_LogicalExpression(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: or.h:207
Rcpp::VectorBase< LGLSXP, LHS_NA, LHS_T > LHS_TYPE
Definition: or.h:204
Or_SingleLogicalResult_SingleLogicalResult(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: or.h:79
SingleLogicalResult< LHS_NA, Or_SingleLogicalResult_SingleLogicalResult< LHS_NA, LHS_T, false, RHS_T > > BASE
Definition: or.h:77
Or_SingleLogicalResult_SingleLogicalResult(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: or.h:115
SingleLogicalResult< RHS_NA, Or_SingleLogicalResult_SingleLogicalResult< false, LHS_T, RHS_NA, RHS_T > > BASE
Definition: or.h:113
SingleLogicalResult< false, Or_SingleLogicalResult_SingleLogicalResult< false, LHS_T, false, RHS_T > > BASE
Definition: or.h:148
Or_SingleLogicalResult_SingleLogicalResult(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: or.h:150
SingleLogicalResult< LHS_NA, LHS_T > LHS_TYPE
Definition: or.h:36
SingleLogicalResult< RHS_NA, RHS_T > RHS_TYPE
Definition: or.h:37
Or_SingleLogicalResult_SingleLogicalResult(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: or.h:43
Or_SingleLogicalResult_bool(const LHS_TYPE &lhs_, bool rhs_)
Definition: or.h:183
SingleLogicalResult< LHS_NA, LHS_T > LHS_TYPE
Definition: or.h:177
SingleLogicalResult< LHS_NA, Or_SingleLogicalResult_bool< LHS_NA, LHS_T > > BASE
Definition: or.h:181
bool is_na< LGLSXP >(int x)
Definition: is_na.h:57
Rcpp API.
Definition: algo.h:28
Rcpp::sugar::Or_SingleLogicalResult_SingleLogicalResult< LHS_NA, LHS_T, RHS_NA, RHS_T > operator||(const Rcpp::sugar::SingleLogicalResult< LHS_NA, LHS_T > &lhs, const Rcpp::sugar::SingleLogicalResult< LHS_NA, LHS_T > &rhs)
Definition: or.h:287
Rcpp::sugar::Or_LogicalExpression_LogicalExpression< LHS_NA, LHS_T, RHS_NA, RHS_T > operator|(const Rcpp::VectorBase< LGLSXP, LHS_NA, LHS_T > &lhs, const Rcpp::VectorBase< LGLSXP, RHS_NA, RHS_T > &rhs)
Definition: or.h:315