Rcpp Version 1.0.14
Loading...
Searching...
No Matches
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
25namespace Rcpp{
26namespace sugar{
27
28template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
31 (LHS_NA || RHS_NA) ,
32 And_SingleLogicalResult_SingleLogicalResult<LHS_NA,LHS_T,RHS_NA,RHS_T>
33 >
34{
35public:
38 typedef SingleLogicalResult<
39 (LHS_NA || RHS_NA) ,
41 > BASE ;
42
45
46 inline void apply(){
47 int left = lhs.get() ;
49 BASE::set( left ) ;
50 } else if( left == FALSE ){
51 BASE::set( FALSE ) ;
52 } else {
53 BASE::set( rhs.get() ) ;
54 }
55 }
56
57private:
58 const LHS_TYPE& lhs ;
59 const RHS_TYPE& rhs ;
60
61} ;
62
63// special version when we know the rhs is not NA
64template <bool LHS_NA, typename LHS_T, typename RHS_T>
67 LHS_NA ,
68 And_SingleLogicalResult_SingleLogicalResult<LHS_NA,LHS_T,false,RHS_T>
69 >
70{
71public:
74 typedef SingleLogicalResult<
75 LHS_NA,
77 > BASE ;
78
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
92private:
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
100template <typename LHS_T, bool RHS_NA, typename RHS_T>
103 RHS_NA ,
104 And_SingleLogicalResult_SingleLogicalResult<false,LHS_T,RHS_NA,RHS_T>
105 >
106{
107public:
110 typedef SingleLogicalResult<
111 RHS_NA,
114
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
128private:
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
135template <typename LHS_T, typename RHS_T>
138 false ,
139 And_SingleLogicalResult_SingleLogicalResult<false,LHS_T,false,RHS_T>
140 >
141{
142public:
145 typedef SingleLogicalResult<
146 false,
149
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
162private:
163 const LHS_TYPE& lhs ;
164 const RHS_TYPE& rhs ;
165
166} ;
167
168
169
170template <bool LHS_NA, typename LHS_T>
173 LHS_NA ,
174 And_SingleLogicalResult_bool<LHS_NA,LHS_T>
175 >
176{
177public:
179 typedef SingleLogicalResult<
180 LHS_NA ,
183
186
187 inline void apply(){
188 if( !rhs ){
189 BASE::set( FALSE ) ;
190 } else{
191 BASE::set( lhs.get() ) ;
192 }
193 }
194
195private:
196 const LHS_TYPE& lhs ;
197 bool rhs ;
198
199} ;
200
201
202
203// (LogicalExpression) & (LogicalExpression)
204template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
205class And_LogicalExpression_LogicalExpression : public Rcpp::VectorBase< LGLSXP, true, And_LogicalExpression_LogicalExpression<LHS_NA,LHS_T,RHS_NA,RHS_T> >{
206public:
209
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
219private:
220 const LHS_TYPE& lhs ;
221 const RHS_TYPE& rhs ;
222} ;
223template <typename LHS_T, bool RHS_NA, typename RHS_T>
225 : public Rcpp::VectorBase< LGLSXP, true, And_LogicalExpression_LogicalExpression<false,LHS_T,RHS_NA,RHS_T> >{
226public:
229
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
239private:
240 const LHS_TYPE& lhs ;
241 const RHS_TYPE& rhs ;
242} ;
243template <bool LHS_NA, typename LHS_T, typename RHS_T>
245 : public Rcpp::VectorBase< LGLSXP, true, And_LogicalExpression_LogicalExpression<LHS_NA,LHS_T,false,RHS_T> >{
246public:
249
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
259private:
260 const LHS_TYPE& lhs ;
261 const RHS_TYPE& rhs ;
262} ;
263template <typename LHS_T, typename RHS_T>
265 : public Rcpp::VectorBase< LGLSXP, false, And_LogicalExpression_LogicalExpression<false,LHS_T,false,RHS_T> >{
266public:
269
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
278private:
279 const LHS_TYPE& lhs ;
280 const RHS_TYPE& rhs ;
281} ;
282
283}
284}
285
286template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
294
295template <bool LHS_NA, typename LHS_T>
303
304template <bool LHS_NA, typename LHS_T>
312
313// (logical expression) & (logical expression)
314template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
322
323
324#endif
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
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
R_xlen_t size() const
Definition VectorBase.h:49
And_LogicalExpression_LogicalExpression(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition and.h:250
And_LogicalExpression_LogicalExpression(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition and.h:230
And_LogicalExpression_LogicalExpression(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition and.h:270
Rcpp::VectorBase< LGLSXP, RHS_NA, RHS_T > RHS_TYPE
Definition and.h:208
And_LogicalExpression_LogicalExpression(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition and.h:210
Rcpp::VectorBase< LGLSXP, LHS_NA, LHS_T > LHS_TYPE
Definition and.h:207
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
SingleLogicalResult< RHS_NA, And_SingleLogicalResult_SingleLogicalResult< false, LHS_T, RHS_NA, RHS_T > > BASE
Definition and.h:113
And_SingleLogicalResult_SingleLogicalResult(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition and.h:115
SingleLogicalResult< false, And_SingleLogicalResult_SingleLogicalResult< false, LHS_T, false, RHS_T > > BASE
Definition and.h:148
And_SingleLogicalResult_SingleLogicalResult(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition and.h:150
SingleLogicalResult< LHS_NA, LHS_T > LHS_TYPE
Definition and.h:36
SingleLogicalResult< RHS_NA, RHS_T > RHS_TYPE
Definition and.h:37
And_SingleLogicalResult_SingleLogicalResult(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition and.h:43
SingleLogicalResult< LHS_NA, And_SingleLogicalResult_bool< LHS_NA, LHS_T > > BASE
Definition and.h:182
And_SingleLogicalResult_bool(const LHS_TYPE &lhs_, bool rhs_)
Definition and.h:184
SingleLogicalResult< LHS_NA, LHS_T > LHS_TYPE
Definition and.h:178
bool is_na< LGLSXP >(int x)
Definition is_na.h:57
Rcpp API.
Definition algo.h:28
T as(SEXP x)
Definition as.h:151