Rcpp Version 0.12.12
and.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // and.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_and_h
23 #define Rcpp__sugar__logical_and_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  And_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 
43  And_SingleLogicalResult_SingleLogicalResult( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_) :
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 == FALSE ){
51  BASE::set( FALSE ) ;
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 And_SingleLogicalResult_SingleLogicalResult<LHS_NA,LHS_T,false,RHS_T> :
66 public SingleLogicalResult<
67  LHS_NA ,
68  And_SingleLogicalResult_SingleLogicalResult<LHS_NA,LHS_T,false,RHS_T>
69  >
70 {
71 public:
74  typedef SingleLogicalResult<
75  LHS_NA,
77  > BASE ;
78 
79  And_SingleLogicalResult_SingleLogicalResult( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_) :
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 == FALSE ){
86  BASE::set( FALSE ) ;
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 And_SingleLogicalResult_SingleLogicalResult<false,LHS_T,RHS_NA,RHS_T> :
102 public SingleLogicalResult<
103  RHS_NA ,
104  And_SingleLogicalResult_SingleLogicalResult<false,LHS_T,RHS_NA,RHS_T>
105  >
106 {
107 public:
110  typedef SingleLogicalResult<
111  RHS_NA,
113  > BASE ;
114 
115  And_SingleLogicalResult_SingleLogicalResult( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_) :
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 == FALSE ){
122  BASE::set( FALSE ) ;
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 And_SingleLogicalResult_SingleLogicalResult<false,LHS_T,false,RHS_T> :
137 public SingleLogicalResult<
138  false ,
139  And_SingleLogicalResult_SingleLogicalResult<false,LHS_T,false,RHS_T>
140  >
141 {
142 public:
145  typedef SingleLogicalResult<
146  false,
148  > BASE ;
149 
150  And_SingleLogicalResult_SingleLogicalResult( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_) :
151  lhs(lhs_), rhs(rhs_){} ;
152 
153  inline void apply(){
154  int left = lhs.get() ;
155  if( left == FALSE ){
156  BASE::set( FALSE ) ;
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 
170 template <bool LHS_NA, typename LHS_T>
172 public SingleLogicalResult<
173  LHS_NA ,
174  And_SingleLogicalResult_bool<LHS_NA,LHS_T>
175  >
176 {
177 public:
179  typedef SingleLogicalResult<
180  LHS_NA ,
182  > BASE ;
183 
184  And_SingleLogicalResult_bool( const LHS_TYPE& lhs_, bool rhs_) :
185  lhs(lhs_), rhs(rhs_){} ;
186 
187  inline void apply(){
188  if( !rhs ){
189  BASE::set( FALSE ) ;
190  } else{
191  BASE::set( lhs.get() ) ;
192  }
193  }
194 
195 private:
196  const LHS_TYPE& lhs ;
197  bool rhs ;
198 
199 } ;
200 
201 
202 
203 // (LogicalExpression) & (LogicalExpression)
204 template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
205 class And_LogicalExpression_LogicalExpression : public Rcpp::VectorBase< LGLSXP, true, And_LogicalExpression_LogicalExpression<LHS_NA,LHS_T,RHS_NA,RHS_T> >{
206 public:
209 
210  And_LogicalExpression_LogicalExpression( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : lhs(lhs_), rhs(rhs_){}
211 
212  inline int operator[]( R_xlen_t i ) const{
213  if( lhs[i] == TRUE && rhs[i] == TRUE ) return TRUE ;
214  if( lhs[i] == NA_LOGICAL || rhs[i] == NA_LOGICAL ) return NA_LOGICAL ;
215  return FALSE ;
216  }
217  inline R_xlen_t size() const { return lhs.size(); }
218 
219 private:
220  const LHS_TYPE& lhs ;
221  const RHS_TYPE& rhs ;
222 } ;
223 template <typename LHS_T, bool RHS_NA, typename RHS_T>
224 class And_LogicalExpression_LogicalExpression<false,LHS_T,RHS_NA,RHS_T>
225  : public Rcpp::VectorBase< LGLSXP, true, And_LogicalExpression_LogicalExpression<false,LHS_T,RHS_NA,RHS_T> >{
226 public:
229 
230  And_LogicalExpression_LogicalExpression( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : lhs(lhs_), rhs(rhs_){}
231 
232  inline int operator[]( R_xlen_t i ) const{
233  if( lhs[i] == TRUE && rhs[i] == TRUE ) return TRUE ;
234  if( rhs[i] == NA_LOGICAL ) return NA_LOGICAL ;
235  return FALSE ;
236  }
237  inline R_xlen_t size() const { return lhs.size(); }
238 
239 private:
240  const LHS_TYPE& lhs ;
241  const RHS_TYPE& rhs ;
242 } ;
243 template <bool LHS_NA, typename LHS_T, typename RHS_T>
244 class And_LogicalExpression_LogicalExpression<LHS_NA,LHS_T,false,RHS_T>
245  : public Rcpp::VectorBase< LGLSXP, true, And_LogicalExpression_LogicalExpression<LHS_NA,LHS_T,false,RHS_T> >{
246 public:
249 
250  And_LogicalExpression_LogicalExpression( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : lhs(lhs_), rhs(rhs_){}
251 
252  inline int operator[]( R_xlen_t i ) const{
253  if( lhs[i] == TRUE && rhs[i] == TRUE ) return TRUE ;
254  if( lhs[i] == NA_LOGICAL ) return NA_LOGICAL ;
255  return FALSE;
256  }
257  inline R_xlen_t size() const { return lhs.size(); }
258 
259 private:
260  const LHS_TYPE& lhs ;
261  const RHS_TYPE& rhs ;
262 } ;
263 template <typename LHS_T, typename RHS_T>
264 class And_LogicalExpression_LogicalExpression<false,LHS_T,false,RHS_T>
265  : public Rcpp::VectorBase< LGLSXP, false, And_LogicalExpression_LogicalExpression<false,LHS_T,false,RHS_T> >{
266 public:
269 
270  And_LogicalExpression_LogicalExpression( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : lhs(lhs_), rhs(rhs_){}
271 
272  inline int operator[]( R_xlen_t i ) const{
273  if( lhs[i] == TRUE && rhs[i] == TRUE ) return TRUE ;
274  return FALSE;
275  }
276  inline R_xlen_t size() const { return lhs.size(); }
277 
278 private:
279  const LHS_TYPE& lhs ;
280  const RHS_TYPE& rhs ;
281 } ;
282 
283 }
284 }
285 
286 template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
291 ){
293 }
294 
295 template <bool LHS_NA, typename LHS_T>
299  bool rhs
300 ){
302 }
303 
304 template <bool LHS_NA, typename LHS_T>
307  bool rhs,
309 ){
311 }
312 
313 // (logical expression) & (logical expression)
314 template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
319 ){
321 }
322 
323 
324 #endif
And_SingleLogicalResult_SingleLogicalResult(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: and.h:150
SingleLogicalResult< RHS_NA, RHS_T > RHS_TYPE
Definition: and.h:37
Rcpp::sugar::And_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: and.h:316
And_LogicalExpression_LogicalExpression(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: and.h:250
Rcpp::sugar::And_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: and.h:288
bool is_na< LGLSXP >(int x)
Definition: is_na.h:57
SingleLogicalResult< LHS_NA, LHS_T > LHS_TYPE
Definition: and.h:178
And_LogicalExpression_LogicalExpression(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: and.h:230
Rcpp::VectorBase< LGLSXP, LHS_NA, LHS_T > LHS_TYPE
Definition: and.h:207
And_SingleLogicalResult_bool(const LHS_TYPE &lhs_, bool rhs_)
Definition: and.h:184
SingleLogicalResult< LHS_NA, And_SingleLogicalResult_bool< LHS_NA, LHS_T > > BASE
Definition: and.h:182
SingleLogicalResult< RHS_NA, And_SingleLogicalResult_SingleLogicalResult< false, LHS_T, RHS_NA, RHS_T > > BASE
Definition: and.h:113
Rcpp API.
Definition: algo.h:28
Rcpp::VectorBase< LGLSXP, RHS_NA, RHS_T > RHS_TYPE
Definition: and.h:208
SingleLogicalResult< false, And_SingleLogicalResult_SingleLogicalResult< false, LHS_T, false, RHS_T > > BASE
Definition: and.h:148
SingleLogicalResult< LHS_NA, LHS_T > LHS_TYPE
Definition: and.h:36
And_SingleLogicalResult_SingleLogicalResult(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: and.h:115
And_LogicalExpression_LogicalExpression(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: and.h:210
SingleLogicalResult< LHS_NA, And_SingleLogicalResult_SingleLogicalResult< LHS_NA, LHS_T, false, RHS_T > > BASE
Definition: and.h:77
And_SingleLogicalResult_SingleLogicalResult(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: and.h:79
And_SingleLogicalResult_SingleLogicalResult(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: and.h:43
And_LogicalExpression_LogicalExpression(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition: and.h:270